【问题标题】:Saving Image to file from imagePickerController - unable to load image从 imagePickerController 将图像保存到文件 - 无法加载图像
【发布时间】:2014-12-23 12:31:38
【问题描述】:

我不太确定这里出了什么问题,我真的很感激一些帮助,因为我非常卡住。 这是我的imagePickerController

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithLong:customphoto1] forKey:@"customphoto1count"];
        UIImage *button1Image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


    pngData = UIImagePNGRepresentation(button1Image);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Button1image.png"]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
    [self dismissViewControllerAnimated:YES completion:NULL];
    pngData = [NSData dataWithContentsOfFile:filePath];
    UIImage *imageLoaded = [UIImage imageWithData:pngData];
    [_button1 setImage:imageLoaded forState:UIControlStateNormal];
    self.button1.clipsToBounds = YES;
    self.button1.layer.cornerRadius = (self.button1.frame.size.width / 16);//half of the width
    self.button1.layer.borderColor=[UIColor blackColor].CGColor;
    self.button1.layer.borderWidth=1.0f;
    [self dismissViewControllerAnimated:YES completion:NULL];
}

上面保存了图像(据我所知),因为_button1 正在从imageLoaded 读取

当应用关闭并重新打开时,这就是要显示的按钮,应该从我的 NSData 加载图片:

-(void)customButton1{
    if (customphoto1 == 1) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Button1image.png"]; //Add the file name
        [pngData writeToFile:filePath atomically:YES];
        pngData = [NSData dataWithContentsOfFile:filePath];
        UIImage *imageLoaded1 = [UIImage imageWithData:pngData];
        NSLog(@"%@", filePath);
//        UIImage *imageload1 = [UIImage imageWithData:[newManagedObject valueForKey:@"imagesone"]];
        [_button1 setImage:imageLoaded1 forState:UIControlStateNormal];

    }
    else {
        button1ImageName = @"addnew";
    }
}

为什么我的图片没有加载? 非常感谢

【问题讨论】:

  • 从 Documents 目录加载图片时,您确认文件路径正确吗?
  • 为什么你给[self dismissViewControllerAnimated:YES completion:NULL];打了两次电话?
  • @Manesh 请使用这个 [self dismissViewControllerAnimated:YES completion:NULL];在最后的方法中行一次。让它使用这个编辑过的代码并告诉我。
  • @AdeelUrRehman 我已经删除了重复的dismissViewController,只保留了最后一个。程序仍然无法加载图像。
  • @SandeepAhuja 尝试过但没有任何作用

标签: ios xcode uiimagepickercontroller nsdata


【解决方案1】:

试试这个:

-(void)customButton1{
    if (customphoto1 == 1) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Button1image.png"]; 
        UIImage *imageLoaded1 = [UIImage imagewithcontentsoffile:filePath];
        NSLog(@"%@", filePath);

        [_button1 setImage:imageLoaded1 forState:UIControlStateNormal];

    }
    else {
        button1ImageName = @"addnew";
    }
}

调试:检查正在打印的文件路径是否正确。在 Finder > CMD + Shift + G 中打开此路径并粘贴路径(从路径中删除文件名,直到文件夹级别使用)。然后检查该文件夹中是否有该特定图像。

同样在您的didFinishPickingMediaWithInfo 方法中,您调用了两次dismissViewControllerAnimated 方法,应该是一次并在方法的末尾。

希望对你有帮助

【讨论】:

  • 恐怕图像仍然是空的。
  • 对不起,我以前没有这样做过,所以我为任何愚蠢道歉。我是否将“/var/mobile/Containers/Data/Application/B7A2B1CC-5F2C-4170-96E0-B42E5AAC8ACD/Documents/”放入“转到文件夹”搜索中?
  • 找不到文件夹
  • 我正在使用 iExplorer 检查应用程序名称文件夹,在应用程序的 Documents 文件夹中,可以看到 Button1image.png。即使应用程序已关闭,它仍然存在。 @mrunal
【解决方案2】:

图像一直保存和加载正常 - 问题原来是按钮 setImage 功能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-22
    • 2015-07-03
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多