muthukumaresh
muthukumaresh

Reputation: 103

How to show images that are stored in document directory to iphone gallery?

i am taking screenshot of my view and its getting stored in document directory. But i need to display the stored image in iphone gallery app. i dont know how to pass images to gallery view. help me in this. thank u in adv..

- (IBAction)Screenshot:(id)sender {

    CGSize targetImageSize = CGSizeMake(500, 500);
    // Check for retina image rendering option
    if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(targetImageSize, NO, 0);
    else UIGraphicsBeginImageContext(targetImageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();
    // The view to be rendered
    [[image layer] renderInContext:context];
    // Get the rendered image
    UIImage *original_image = UIGraphicsGetImageFromCurrentImageContext();
    NSLog(@"%@",original_image);
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/screenshots"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
    //Create folder
//    NSString *documentsDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

   NSString *pngFilePath = [NSString stringWithFormat:@"%@/myPngFile.png",dataPath];
   [UIImagePNGRepresentation(original_image) writeToFile:pngFilePath atomically:YES];
    //[_image1 setImage:original_image];
    UIGraphicsEndImageContext();
}

Upvotes: 0

Views: 889

Answers (4)

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

Use below code for stored image into gallery

UIImageWriteToSavedPhotosAlbum(imgNewImage, nil, nil, nil);

imgNewImage is your UIImage object.

Upvotes: 0

Abi
Abi

Reputation: 128

You can use this function:

UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                           id completionTarget, 
                           SEL completionSelector, 
                           void *contextInfo);

You only need completionTarget, completionSelector and contextInfo if you want to be notified when the UIImage is done saving, otherwise you can pass in nil.

For Example :

-(void)savePhoto {
   NSURL *imageURL = receivedURL;
   UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
   UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
   [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)   savedPhotoImage:(UIImage *)image
  didFinishSavingWithError:(NSError *)error
           contextInfo:(void *)contextInfo
{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                message:@"This image has been saved to your photo album"
                                               delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [alert show];
}

Upvotes: 3

Anand
Anand

Reputation: 22

Here i have pass String variable in anotherview same way you can use for image. for image code is as below:

UserDetailsViewController *userDetailsViewController = [[UserDetailsViewController alloc] initWithNibName:@"UserDetailsViewController" bundle:nil];
userDetailsViewController.img = [UIImage imageNamed:@"abc.png"];
[self.navigationController pushViewController:userDetailsViewController animated:YES];

Upvotes: 0

Geet
Geet

Reputation: 2437

You have to store the image in Photo Album if you need to see it in the iPhone Photo App use this code

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    UIImage *image=//what ever Image you wan tot save and view in photo album of iPhone;

 [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[SAVEIMAGE imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            // TODO: error handling

        } else {




            // TODO: success handling
        }

    }];

Upvotes: 0

Related Questions