ALAssetsLibrary
其層次關系為
注意:
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,在設置->通用中),返回的錯誤為ALAssetsLibraryAccessGlo ballyDeniedError 。enumerationBlock和failureBlock與在調用此方法的線程內執行,若要在背景線程進行遍歷,可將遍歷代碼放入GCD或NSOperation中。 [assetsLibrary enumerateGroupsWithTypes
:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (!group) { [tableView performSelectorOnMainThr ead:@selector(reloadData) withObject:nil waitUntilDone:NO]; }else{ [groupsArray addObject:group]; ... } } failureBlock:^(NSError *error) { NSLog(@"error:%@",error); }]; 四.遍歷Assets Group中的Assets
使用enumerateAssetsUsingBloc k: 方法或者其他變體方法可遍歷ALAssetsGroup中的所有ALAsset;可通過 setAssetsFilter: 設置過濾器( ALssetsFilter )使enumerateAssetsUsingBloc k:只返回特定類型asset,而 numberOfAssets 只返回特定類型asset的數量。 可以設置只顯示Photo( allPhotos ),只顯示Video( allVideos ),或顯示全部(allAssets )。enumerateAssetsUsingBloc k:為同步方法,只有所有Asset遍歷完才返回。所以需要將遍歷代碼放入背景線程,防止阻塞UI線程。 [assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [assetsGroup enumerateAssetsUsingBloc
k:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (!result) { [tableView performSelectorOnMainThr ead:@selector(reloadData) withObject:nil waitUntilDone:NO]; }else{ [assetsArray addObject:result]; ... } }]; }); 五.根據url獲取asset
使用ALAssetsLibrary的
assetForURL:resultBlock:failureBlock: 方法,可根據之前從ALAssetRepresentation中獲取的url重新獲取ALAsset對象,此方法同enumerateGroupsWithTypes :usingBlock:failureBlock:一樣為異步方法。 六.獲取Assets的各種屬性
相冊封面圖片 [assetsGroupposterImage ];照片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];
新聞熱點
疑難解答