国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用ALAssetsLibrary讀取所有照片

2019-11-09 18:48:01
字體:
來源:轉載
供稿:網友

一. ALAssetsLibrary 介紹

ALAssetsLibrary 提供了訪問iOS設備下”照片”應用下所有照片和視頻的接口;

從 ALAssetsLibrary 中可讀取所有的相冊數據,即 ALAssetsGroup 對象列表;從每個 ALAssetsGroup 中可獲取到其中包含的照片或視頻列表,即 ALAsset 對象列表;每個 ALAsset 可能有多個rePResentations表示,即 ALAssetRepresentation 對象,使用其 defaultRepresentation 方法可獲得其默認representations,使用[asset valueForProperty: ALAssetPropertyRepresentations ]可獲取其所有representations的 UTI 數組。從ALAsset對象可獲取縮略圖 thumbnail 或 aspectRatioThumbnail ;從 ALAssetRepresentation 對象可獲取全尺寸圖片( fullResolutionImage ),全屏圖片( fullScreenImage )及圖片的各種屬性: orientation , dimensions, scale , url , metadata 等。

其層次關系為 ALAssetsLibrary -> ALAssetsGroup -> ALAsset ->ALAssetRepresentation 。

注意:

The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.

通過ALAssetsLibrary對象獲取的其他對象只在該ALAssetsLibrary對象生命期內有效,若ALAssetsLibrary對象被銷毀,則其他從它獲取的對象將不能被訪問,否則有會錯誤。 

invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibraryALAssetRepresentation的 metadata 方法很慢,我在iphone4 iOS5.1.1中測試,此方法返回需要40-50ms,所以獲取圖片的個各種屬性盡量直接從ALAssetRepresentation中獲取,不要讀取metadata。 這里 給出了一個此方法占用內存過多的解釋,調用多次也確實很容易會memory warning,或許也能解析其為什么很慢吧。

The method [representation metadata] returns an autoreleased object and possibly creates more autoreleased objects when it executes. All these instances are added to the autorelease pool, waiting to be finally released (and their memory freed) when the ARP gets the chance to drain itself.

系統”相冊”程序顯示的圖片是 fullScreenImage ,而不是 fullResolutionImage ,fullResolutionImage尺寸太大,在手機端顯示推薦用fullScreenImage。 fullScreenImage已被調整過方向,可直接使用,即
[UIImage imageWithCGImage:representation.fullScreenImage];

使用fullResolutionImage要自己調整方法和scale,即

[UIImage imageWithCGImage:representation.fullResolutionImage scale:representation.scale orientation:representation.orientation];

二.創建 ALAssetsLibrary對象

使用ALAssetsLibrary之前需導入頭文件和AssetsLibrary.framework。

  #import      ...  ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];  ... 

三.遍歷Assets Group

使用 enumerateGroupsWithTypes:usingBlock:failureBlock: 方法可遍歷assets group;此方法為異步執行,若之前未被授權過,此方法會向用戶請求訪問數據的權限;若用戶拒絕授權或其他錯誤則會執行failureBlock;如果用戶關掉了位置服務(Location Services,在設置->通用中),返回的錯誤為ALAssetsLibraryAccessGloballyDeniedError 。enumerationBlock和failureBlock與在調用此方法的線程內執行,若要在背景線程進行遍歷,可將遍歷代碼放入GCD或NSOperation中。
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {        if (!group) {            [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];        }else{            [groupsArray addObject:group];            ...        }    } failureBlock:^(NSError *error) {        NSLog(@"error:%@",error);    }];

四.遍歷Assets Group中的Assets

使用 enumerateAssetsUsingBlock: 方法或者其他變體方法可遍歷ALAssetsGroup中的所有ALAsset;可通過 setAssetsFilter: 設置過濾器( ALssetsFilter )使enumerateAssetsUsingBlock:只返回特定類型asset,而 numberOfAssets 只返回特定類型asset的數量。 可以設置只顯示Photo( allPhotos ),只顯示Video( allVideos ),或顯示全部(allAssets )。enumerateAssetsUsingBlock:為同步方法,只有所有Asset遍歷完才返回。所以需要將遍歷代碼放入背景線程,防止阻塞UI線程。
    [assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        [assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {            if (!result) {                [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];            }else{                [assetsArray addObject:result];                ...            }        }];    });

五.根據url獲取asset

使用ALAssetsLibrary的 assetForURL:resultBlock:failureBlock: 方法,可根據之前從ALAssetRepresentation中獲取的url重新獲取ALAsset對象,此方法同enumerateGroupsWithTypes:usingBlock:failureBlock:一樣為異步方法。

六.獲取Assets的各種屬性

相冊封面圖片 [assetsGroup posterImage ];照片url[[[asset defaultRepresentation] url] absoluteString];照片縮略圖 [asset thumbnail]; [asset aspectRatioThumbnail];照片全尺寸圖 [[asset defaultRepresentation] fullResolutionImage];照片全屏圖 [[asset defaultRepresentation] fullScreenImage];照片創建時間 [asset valueForProperty:ALAssetPropertyDate];照片拍攝位置(可能為nil) [asset valueForProperty:ALAssetPropertyLocation];照片尺寸 [[asset defaultRepresentation] dimensions];
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四子王旗| 唐海县| 周口市| 阆中市| 舟曲县| 岑溪市| 临海市| 永年县| 方山县| 水城县| 富平县| 菏泽市| 花垣县| 理塘县| 祥云县| 九寨沟县| 崇左市| 福安市| 辽宁省| 定兴县| 弥渡县| 包头市| 婺源县| 裕民县| 全椒县| 景宁| 永嘉县| 舟曲县| 固原市| 双城市| 嵊泗县| 乡宁县| 兴和县| 尼木县| 南乐县| 门头沟区| 永胜县| 鞍山市| 鞍山市| 南川市| 荃湾区|