简体   繁体   English

将图像保存在UIImagePickerController中

[英]Save image in UIImagePickerController

I have a small question about how to save a picture in the UIImagePickerController. 我有一个关于如何在UIImagePickerController中保存图片的小问题。

I have tried it with the following code: 我已经尝试使用以下代码:

- (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];
} }

I get no errors, but the image is not saved 我没有错误,但是图像没有保存

Thank you very much 非常感谢你

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


    }

This is everything you need to save to the photos. 这就是您需要保存到照片的所有内容。 If you're looking to save it as an uiimage look below: 如果您希望将其另存为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];

}

The problem here seems to be that you are not setting a completion target. 这里的问题似乎是您没有设置完成目标。

UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

This works perfectly for me and so should fix your problem. 这对我来说非常有效,因此应该解决您的问题。

You can also set a completion target to check that it is being saved correctly: 您还可以设置完成目标以检查其是否正确保存:

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

// Check if there was an error //检查是否有错误

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

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

    }
    // All is well
    else {

    }
}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM