How to Save Image to Application Folder from UIIma

2019-08-26 05:58发布

问题:

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.

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;

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

回答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];

   }
}


回答2:

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
}


回答3:

Save an UIImage to the to Documentsfolder like this:

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


回答4:

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


回答5:

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


回答6:

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