从ImagePickerController将图像保存到文件 - 无法加载图像

时间:2014-12-23 12:31:38

标签: ios xcode uiimagepickercontroller nsdata

我不太清楚这里出了什么问题,因为我很困难,所以我真的很感激。 这是我的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";
    }
}

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

2 个答案:

答案 0 :(得分: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方法两次,它应该是一次并且在方法结束时。

希望这有帮助

答案 1 :(得分:0)

图片一直在保存并且一直正常加载 - 问题原来是按钮setImage功能