ALAssetsLibrary-代码操作iOS相册资源
2019獨角獸企業重金招聘Python工程師標準>>>
在iOS中,我們調用攝像頭和選擇相冊中的資源,我們可以使用:UIImagePickerController類來完成。
當然,我們也可以不使用UI的形式來訪問iOS設備的相冊資源。
那就是使用:ALAssetsLibrary
一、ALAssetsLibrary是什么
可以說,是一個橋梁把。連接了我們應用程序和相冊之間的訪問。
ALAssetsLibrary提供了我們對iOS設備中的相片、視頻的訪問。
ALAssetsLibrary被封裝在?框架中。所以,我們在使用時,需要引入該框架。
貼:
self.view.backgroundColor?= [UIColor?whiteColor];
????self.assetsLibrary?= [[ALAssetsLibrary?alloc]?init];
????dispatch_queue_t?dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,?0);
????dispatch_async(dispatchQueue, ^(void) {
????????//?遍歷所有相冊
????????[self.assetsLibrary?enumerateGroupsWithTypes:ALAssetsGroupAll
??????????????????????????????????????????usingBlock:^(ALAssetsGroup?*group,?BOOL*stop) {
??????????????????????????????????????????????//?遍歷每個相冊中的項ALAsset
??????????????????????????????????????????????[group?enumerateAssetsUsingBlock:^(ALAsset?*result,?NSUInteger?index,BOOL?*stop) {
??????????????????????????????????????????????????
??????????????????????????????????????????????????__block?BOOL?foundThePhoto =NO;
??????????????????????????????????????????????????if?(foundThePhoto){
??????????????????????????????????????????????????????*stop =?YES;
??????????????????????????????????????????????????}
??????????????????????????????????????????????????// ALAsset的類型
??????????????????????????????????????????????????NSString?*assetType = [resultvalueForProperty:ALAssetPropertyType];
??????????????????????????????????????????????????if?([assetTypeisEqualToString:ALAssetTypePhoto]){
??????????????????????????????????????????????????????foundThePhoto =?YES;
??????????????????????????????????????????????????????*stop =?YES;
??????????????????????????????????????????????????????ALAssetRepresentation*assetRepresentation =[result?defaultRepresentation];
??????????????????????????????????????????????????????CGFloat?imageScale = [assetRepresentation?scale];
??????????????????????????????????????????????????????UIImageOrientationimageOrientation = (UIImageOrientation)[assetRepresentation?orientation];
?????????????????????????????????????????????????????dispatch_async(dispatch_get_main_queue(), ^(void) {
??????????????????????????????????????????????????????????CGImageRefimageReference = [assetRepresentation?fullResolutionImage];
??????????????????????????????????????????????????????????//?對找到的圖片進行操作
??????????????????????????????????????????????????????????UIImage?*image =[[UIImage?alloc]?initWithCGImage:imageReference?scale:imageScaleorientation:imageOrientation];
??????????????????????????????????????????????????????????if?(image !=?nil){
??????????????????????????????????????????????????????????????self.imageView?= [[UIImageView?alloc]?initWithFrame:self.view.bounds];
?????????????????????????????????????????????????????????????self.imageView.contentMode?=?UIViewContentModeScaleAspectFit;
?????????????????????????????????????????????????????????????self.imageView.image?= image;
??????????????????????????????????????????????????????????????[self.viewaddSubview:self.imageView];
??????????????????????????????????????????????????????????}?else?{
??????????????????????????????????????????????????????????????NSLog(@"Failed to create the image.");
??????????????????????????????????????????????????????????} });
??????????????????????????????????????????????????}
??????????????????????????????????????????????}];
??????????????????????????????????????????}
??????????????????????????????????????????failureBlock:^(NSError?*error) {
??????????????????????????????????????????????NSLog(@"Failed to enumerate the asset groups.");
??????????????????????????????????????????}];
????????
????});
亂啊,沒辦法,在xCode中是OK的。
需要解釋的幾點:
1.流程:該代碼的流程,就是先遍歷所有相冊,然后,遍歷每個相冊中的第一張圖片。
2.ALAssetsGroup:指代一個相冊。
3.ALAsset:每一個ALAsset代表一個單一資源文件(也就是一張圖片,或者一個視頻文件)
4.ALAssetRepresentation:ALAssetRepresentation封裝了ALAsset,包含了一個資源文件中的很多屬性。(可以說是ALAsset的不同的表示方式,本質上都表示同一個資源文件)
轉載于:https://my.oschina.net/starmier/blog/200077
總結
以上是生活随笔為你收集整理的ALAssetsLibrary-代码操作iOS相册资源的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到牙齿掉了好多颗是什么意思
- 下一篇: 放学默示录Ⅰ:来!抠奖 移动游戏策划案