0

我有一个关于如何在 UIImagePickerController 中保存图片的小问题。

我已经使用以下代码进行了尝试:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image didFinishPickingMediaWithInfo:(NSDictionary *)info {

if (takePhotoSave == 1) {
    takePhotoSave = 0;

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [picker dismissViewControllerAnimated:YES completion:NULL];
} }

我没有收到任何错误,但图像未保存

非常感谢

4

2 回答 2

0

这里的问题似乎是您没有设置完成目标。

UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

这对我来说非常有效,因此应该可以解决您的问题。

您还可以设置完成目标以检查它是否正确保存:

// Save image
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:), nil);

// 检查是否有错误

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error {

    // Unable to save the image
    if (error) {

    }
    // All is well
    else {

    }
}

希望这可以帮助

于 2015-01-27T00:19:46.443 回答
0
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:NO completion:nil];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

        //save to document directory
        NSData *pngData = UIImagePNGRepresentation([self imageWithImage:image scaledToSize:CGSizeMake(image.size.width/5, image.size.height/5)]);
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image_test.png"]; //Add the file name
        [pngData writeToFile:filePath atomically:YES]; //Write the file


    }

这是您需要保存到照片的所有内容。如果您希望将其保存为 uiimage,请查看以下内容:

if ([NSData dataWithContentsOfFile:[self documentsPathForFileName:@"image_test.png"]]) {

    NSData* pngData = [NSData dataWithContentsOfFile:[self documentsPathForFileName:@"image_test.png"]];
    UIImage* image = [UIImage imageWithData:pngData];


    UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0.0, 0.0, imageView.image.size.width, imageView.image.size.height)];
    [imageView setCenter:self.view.center];
    [self.view addSubview:imageView];

}
于 2013-09-17T19:09:40.373 回答