ALAssetsLibrary-代码操作iOS相册资源
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
在iOS中,我們調(diào)用攝像頭和選擇相冊(cè)中的資源,我們可以使用:UIImagePickerController類來(lái)完成。
當(dāng)然,我們也可以不使用UI的形式來(lái)訪問(wèn)iOS設(shè)備的相冊(cè)資源。
那就是使用:ALAssetsLibrary
一、ALAssetsLibrary是什么
可以說(shuō),是一個(gè)橋梁把。連接了我們應(yīng)用程序和相冊(cè)之間的訪問(wèn)。
ALAssetsLibrary提供了我們對(duì)iOS設(shè)備中的相片、視頻的訪問(wèn)。
ALAssetsLibrary被封裝在?框架中。所以,我們?cè)谑褂脮r(shí),需要引入該框架。
貼:
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) {
????????//?遍歷所有相冊(cè)
????????[self.assetsLibrary?enumerateGroupsWithTypes:ALAssetsGroupAll
??????????????????????????????????????????usingBlock:^(ALAssetsGroup?*group,?BOOL*stop) {
??????????????????????????????????????????????//?遍歷每個(gè)相冊(cè)中的項(xiàng)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];
??????????????????????????????????????????????????????????//?對(duì)找到的圖片進(jìn)行操作
??????????????????????????????????????????????????????????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.");
??????????????????????????????????????????}];
????????
????});
亂啊,沒(méi)辦法,在xCode中是OK的。
需要解釋的幾點(diǎn):
1.流程:該代碼的流程,就是先遍歷所有相冊(cè),然后,遍歷每個(gè)相冊(cè)中的第一張圖片。
2.ALAssetsGroup:指代一個(gè)相冊(cè)。
3.ALAsset:每一個(gè)ALAsset代表一個(gè)單一資源文件(也就是一張圖片,或者一個(gè)視頻文件)
4.ALAssetRepresentation:ALAssetRepresentation封裝了ALAsset,包含了一個(gè)資源文件中的很多屬性。(可以說(shuō)是ALAsset的不同的表示方式,本質(zhì)上都表示同一個(gè)資源文件)
轉(zhuǎn)載于:https://my.oschina.net/starmier/blog/200077
總結(jié)
以上是生活随笔為你收集整理的ALAssetsLibrary-代码操作iOS相册资源的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到牙齿掉了好多颗是什么意思
- 下一篇: 梦到怀孕去打胎是什么意思