gpt4 book ai didi

ios - 从 Photos.app 获取最后一张图片?

转载 作者:IT王子 更新时间:2023-10-29 07:35:53 31 4
gpt4 key购买 nike

我看到其他应用程序也这样做,您可以从照片应用程序导入最后一张照片以便快速使用,但据我所知,我只知道如何获取 A 图像而不是最后一张照片(最近一张)。谁能告诉我如何获得最后一张图片?

最佳答案

此代码段将从相机胶卷中获取最新图像(iOS 7 及以下版本):

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

iOS 8 及更高版本:

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
[[PHImageManager defaultManager] requestImageForAsset:lastAsset
targetSize:self.photoLibraryButton.bounds.size
contentMode:PHImageContentModeAspectFill
options:PHImageRequestOptionsVersionCurrent
resultHandler:^(UIImage *result, NSDictionary *info) {

dispatch_async(dispatch_get_main_queue(), ^{

[[self photoLibraryButton] setImage:result forState:UIControlStateNormal];

});
}];

关于ios - 从 Photos.app 获取最后一张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8867496/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com