How do I get back a saved picture on iPhone camera roll?

南笙酒味 提交于 2019-12-25 01:58:02

问题


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.


回答1:


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");



回答2:


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


来源:https://stackoverflow.com/questions/22428615/how-do-i-get-back-a-saved-picture-on-iphone-camera-roll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!