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

时间:2015-02-13 09:12:37

标签: ios objective-c iphone ios8 uiimagepickercontroller

我想到一个让我发疯的问题。我已经搜索了很多东西来解决它,但我找到的所有答案都是陈旧的,没有帮助。

我正在使用 UIImagePickerControllerDelegate 并使用 didFinishPickingMediaWithInfo 处理应用程序我想将所选图像保存到应用程序文件夹。

到目前为止,我可以用这个选择图像;

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

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

感谢您提供的任何帮助。

6 个答案:

答案 0 :(得分:1)

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

   }
}

答案 1 :(得分:0)

试试这段代码。它可能会帮助你。 :)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

将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;
}

答案 4 :(得分:0)

只需将图片保存在您的app文档目录中:

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

答案 5 :(得分:0)

我在我的项目中做到了这一点:

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