0

I am new to iOS development and am trying to create a view that will allow the user to take a picture using their camera.

What I want to happen is when the user has taken the picture, it saves it to their camera roll. I believe that can be simply accomplished by the following statement:

//Let's say the image you want to save is in a UIImage called "imageToBeSaved"
UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);

However, my question is, if the user leaves the view and then returns to it I still want that picture to be there. So how do I get back the same picture I have just taken/saved previously, so I can reload it when they reopen the view?

Any help would be great! Thank you.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Stephen Bennett
  • 431
  • 6
  • 17
  • you can user imagepicker for getting that image from photo album. or programmatically you can fetch the last captured image from album – Pawan Rai Mar 15 '14 at 19:27
  • How would I grab the specific image using imagepicker? Does each image have an id when it is saved? – Stephen Bennett Mar 15 '14 at 19:33
  • Actually you can save image to the Documents directory and read from this directory too. It's a persistent solution because Documents directory is located within your app's bundle and no one other app can access to it. – Igor Matyushkin Mar 15 '14 at 19:45

2 Answers2

0

This code snippet will get the latest image from the camera roll: reference link

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    // Chooses the photo at the last index
    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

        // The end of the enumeration is signaled by asset == nil.
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

            // Stop the enumerations
            *stop = YES; *innerStop = YES;

            // Do something interesting with the AV asset.
            [self sendTweet:latestPhoto];
        }
    }];
} failureBlock: ^(NSError *error) {
    // Typically you should handle an error more gracefully than this.
    NSLog(@"No groups");
}];
Community
  • 1
  • 1
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • I wanted it to grab the same specific image everytime. If the user takes a photo with their camera or another app, wouldn't this code retrieve that image instead because mine would no longer be the last image taken? – Stephen Bennett Mar 15 '14 at 19:38
  • you can save the image with a name , and get that specific image from album. that can be a solution. – Pawan Rai Mar 15 '14 at 19:42
  • i will suggest you to save images from your app to a custom album, and get the last image from your album. – Pawan Rai Mar 15 '14 at 19:43
  • This sounds like a good idea. How would I create a custom album? – Stephen Bennett Mar 15 '14 at 19:45
0
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectoryPath = dirPaths[0];
    NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];

    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:filePath atomically:YES];
}

void UIImageReadFromFile(UIImage **image, NSString *fileName)
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectoryPath = dirPaths[0];
    NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];

    image = [UIImage imageWithContentsOfFile:filePath];
}

Image will be saved to and read from the Documents directory of your app's bundle with specified name.

Usage example:

UIImageWriteToFile(image, @"somephoto.png");

UIImage *fetchedImage;
UIImageReadFromFile(&fetchedImage, @"somephoto.png");
Igor Matyushkin
  • 778
  • 4
  • 4
  • How would I parse a variable into the file name? I can't seem to get it to work. I have the following: UIImageWriteToFile(self.imageView.image, @"%@.png", self.fullname.text); – Stephen Bennett Mar 16 '14 at 20:45