Search code examples
objective-ciphoneios7nsdocumentdirectory

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

Solution

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