在申请文件中保存图像

时间:2011-07-11 15:37:06

标签: iphone xcode uiimage uiimagepickercontroller

我尝试将相机或相册中的图像保存到应用程序目录中。但我无法找到我做错的事。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];       

//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];

//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];  
if ([mediaType isEqualToString:@"public.image"]){
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];


    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
    if(fileExists){
        NSLog(@"image Exists");
    }
}
}

2 个答案:

答案 0 :(得分:1)

为什么不使用,

UIImage *orgImage = [info objectForKey:UIImagePickerControllerOriginalImage];

哪里失败了?你有正确的图像吗?或者没有写入文件目录?

答案 1 :(得分:0)

检查文件是否存在于文件资源管理器中。也许编译器是对的。