gpt4 book ai didi

ios - iOS如何从资源库获取图片?

转载 作者:行者123 更新时间:2023-11-29 01:41:39 25 4
gpt4 key购买 nike

我是 iOS 开发人员,我想从库中获取所有图像,没有 UIImagepickercontroller,并拍摄前 10 张图像有什么想法吗?

最佳答案

这里有很多示例,将指导您如何从 ALAssetLibrary 获取图像

https://www.cocoacontrols.com/search?q=image+picker .

下面是从ImagePicker获取最新图像的示例

- (void)latestPhotoWithCompletion:(void (^)(UIImage *photo))completion
{

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

// For this example, we're only interested in the last item [group numberOfAssets]-1 = last.
if ([group numberOfAssets] > 0) {
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
// Do something interesting with the AV asset.
UIImage *img = [UIImage imageWithCGImage:[representation fullScreenImage]];

// completion
completion(img);

// we only need the first (most recent) photo -- stop the enumeration
*innerStop = YES;
}
}];
}
} failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
}];


}

使用

 __weak __typeof(self)wSelf = self;
[self latestPhotoWithCompletion:^(UIImage *photo) {

UIImageRenderingMode renderingMode = YES ? UIImageRenderingModeAlwaysOriginal : UIImageRenderingModeAlwaysTemplate;
[wSelf.switchCameraBut setImage:[photo imageWithRenderingMode:renderingMode] forState:UIControlStateNormal];

}];

关于ios - iOS如何从资源库获取图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322683/

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