简体   繁体   English

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

[英]How to Save Image to Application Folder from UIImagePickerController?

I've a question in mind that driving me crazy. 我心里有个疑问让我发疯。 I've searched a lot to solve it, but all the answers that I found were old and not helped. 我已经进行了很多搜索来解决它,但是我发现的所有答案都是陈旧且无济于事的。

I'm currently working on application with that uses UIImagePickerControllerDelegate and with didFinishPickingMediaWithInfo I want to save the selected image to application folder. 我目前正在处理使用UIImagePickerControllerDelegatedidFinishPickingMediaWithInfo的应用程序,我想将所选图像保存到应用程序文件夹。

Until now I'm able to select the image with this; 到现在为止,我已经可以选择图像了。

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

}

but couldn't save it to application using didFinishPickingMediaWithInfo; 但无法使用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;
    }
}

I wonder what should be the part after this point. 我不知道在这之后应该扮演什么角色。

I appreciate any help that you can provide. 感谢您提供的任何帮助。

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

   }
}

Try this code. 试试这个代码。 It may helps you. 它可能会帮助您。 :) :)

- (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

Save an UIImage to the to Documentsfolder like this: 像这样将UIImage保存到Documentsfolder中:

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

Get the path of the documents as a string: 以字符串形式获取文档的路径:

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

Just save the image in your app document directory : 只需将图像保存在您的应用程序文档目录中:

// 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];

I did that in my project: 我在我的项目中做到了:

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

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

相关问题 从UIImagepickercontroller中选择后如何保存图像 - How to save image after selecting from UIImagepickercontroller 如何保存从Swift中的UIImagePickerController中拾取的图像? - How to save an image picked from a UIImagePickerController in Swift? 如何将从UIImagePickerController中获取的图像保存为HEIF文件? - How to save image taken from UIImagePickerController as HEIF file? UIImagePickerController-如何将图像保存到核心数据? - UIImagePickerController - how to save image to core data? UIImagePickerController-如何将图像保存到核心数据? (迅速) - UIImagePickerController - how to save image to core data? (Swift) iOS UIImagePickerController如何从相机拍摄图像后立即保存图像 - iOS UIImagePickerController how to save image right after taking image from camera iOS从UIImagePickerController选择图像保存gif图像 - IOS save gif image from UIImagePickerController selected image 如何使用UIAutomation从UIImagePickerController中选择图像 - How to use UIAutomation to select image from UIImagePickerController 如何上传从UIImagePickerController中获取的图像 - How to upload image that was taken from UIImagePickerController 使用UIImagePickerController获取图像 - 如何知道是否保存PNG或JPEG? - Using UIImagePickerController to get image — how to know whether to save PNG or JPEG?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM