如何从 UIImagePickerController 将图像保存到应用程序文件夹?

How to Save Image to Application Folder from UIImagePickerController?

我有一个让我发疯的问题。我已经搜索了很多来解决它,但我发现的所有答案都是旧的并且没有帮助。

我目前正在开发使用 UIImagePickerControllerDelegatedidFinishPickingMediaWithInfo 的应用程序我想保存 selected图像到应用程序文件夹。

到现在为止,我可以select 图像;

- (IBAction)btnPressed:(UIButton *)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
        imgPicker.delegate = self;
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
        imgPicker.allowsEditing = NO;

        [self presentViewController:imgPicker animated:YES completion:nil];
    }

}

但无法使用 didFinishPickingMediaWithInfo 将其保存到应用程序;

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

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
    {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

        //Assigning the selected image to imageView to see on UI side.
        imageViewerImage.image = image;
    }
}

我想知道这一点之后应该是什么部分。

感谢您提供的任何帮助。

试试这个代码。它可能对你有帮助。 :)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imgViewProfile.image = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:^{ }];
    // ------ Now save this image to document directory by getting its path
}
//In Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo . Write this code

NSData *pngData = UIImagePNGRepresentation(image);

//This pulls out PNG data of the image you've captured. From here, you can write it to a file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file

像这样将 UIImage 保存到 Documents 文件夹中:

UIImage* myUIImage = ...
NSData *pngData = UIImagePNGRepresentation(myUIImage);
//NSData *pngData = UIImageJPEGRepresentation(myUIImage,0.5); //alternative comressed jpg instead of png
NSString *filePath = [[self buildDocumentsPath] stringByAppendingPathComponent:@"pictureName.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file

以字符串形式获取文档的路径:

- (NSString *)buildDocumentsPath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    return documentsPath;
}

只需将图像保存在您的应用文档目录中即可:

// first find the path in which to save the image :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *baseDir = [paths objectAtIndex:0];

// then save image as JPEG
NSString* filename = [baseDir stringByAppendingPathComponent:@"filename.jpg"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// or PNG
NSString* filename = [baseDir stringByAppendingPathComponent:@"filename.png"];
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    [self dismissViewControllerAnimated:YES completion:NULL];

UIImage* image;

if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"])
    {

  image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

 NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];

// New Folder is your folder name

 NSError *error = nil;

if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];

NSData *data = UIImageJPEGRepresentation(image, 1.0);

[data writeToFile:fileName atomically:YES];

   }
}

我在我的项目中这样做了:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage * imageDone = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    [self saveImage:imageDone];
    [self.popover dismissPopoverAnimated:YES];
}

-(void)saveImage:(UIImage*)image{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];    
   NSString * imgpath = [documentsDirectory stringByAppendingFormat:@"/media/img/"];

   long x = arc4random() % 10000;
   NSString * namePic = [NSString stringWithFormat:@"%lld%ld-photo.jpg", self.idToLoad.longLongValue, x];
   NSString * stringName = [NSString stringWithFormat:@"%@%@", imgpath, namePic];
   [UIImageJPEGRepresentation(image, 0.3) writeToFile:stringName atomically:YES];
}