简体   繁体   English

如何在照片库中获取最新照片的路径? (作为NSUrl或字符串)

[英]How do I get the path to the most recent photo in the photos library? (As a NSUrl or string)

I need to get the file path to the most recent image in the photo library. 我需要获取照片库中最新图像的文件路径。 How can I do this? 我怎样才能做到这一点?

You can use the AssetsLibrary to access the user's photos. 您可以使用AssetsLibrary访问用户的照片。

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:group.numberOfAssets - 1] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if (result) {
                ALAssetRepresentation *rep;
                rep = [result defaultRepresentation];
                UIImage *image;
                image = [UIImage imageWithCGImage:[rep fullResolutionImage]];

                //Now you can write this UIImage to a file path in your app's directory for future use

                *stop = YES;
            }
        }];
    }
    *stop = NO;
} failureBlock:^(NSError *error) {
    NSLog(@"Error: %@", error);
}];

无法仅访问最新照片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM