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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

使用ALAssetsLibrary讀取所有照片

2019-11-09 17:48:57
字體:
供稿:網(wǎng)友

一. ALAssetsLibrary 介紹

ALAssetsLibrary 提供了訪問iOS設(shè)備下”照片”應(yīng)用下所有照片和視頻的接口;

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

其層次關(guān)系為 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對象生命期內(nèi)有效,若ALAssetsLibrary對象被銷毀,則其他從它獲取的對象將不能被訪問,否則有會錯誤。 

invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibraryALAssetRepresentation的 metadata 方法很慢,我在iphone4 iOS5.1.1中測試,此方法返回需要40-50ms,所以獲取圖片的個各種屬性盡量直接從ALAssetRepresentation中獲取,不要讀取metadata。 這里 給出了一個此方法占用內(nèi)存過多的解釋,調(diào)用多次也確實很容易會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.

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

使用fullResolutionImage要自己調(diào)整方法和scale,即

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

二.創(chuàng)建 ALAssetsLibrary對象

使用ALAssetsLibrary之前需導(dǎo)入頭文件和AssetsLibrary.framework。

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

三.遍歷Assets Group

使用 enumerateGroupsWithTypes:usingBlock:failureBlock: 方法可遍歷assets group;此方法為異步執(zhí)行,若之前未被授權(quán)過,此方法會向用戶請求訪問數(shù)據(jù)的權(quán)限;若用戶拒絕授權(quán)或其他錯誤則會執(zhí)行failureBlock;如果用戶關(guān)掉了位置服務(wù)(Location Services,在設(shè)置->通用中),返回的錯誤為ALAssetsLibraryAccessGloballyDeniedError 。enumerationBlock和failureBlock與在調(diào)用此方法的線程內(nèi)執(zhí)行,若要在背景線程進(jìn)行遍歷,可將遍歷代碼放入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: 設(shè)置過濾器( ALssetsFilter )使enumerateAssetsUsingBlock:只返回特定類型asset,而 numberOfAssets 只返回特定類型asset的數(shù)量。 可以設(shè)置只顯示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];                ...            }        }];    });

五.根據(jù)url獲取asset

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

六.獲取Assets的各種屬性

相冊封面圖片 [assetsGroup posterImage ];照片url[[[asset defaultRepresentation] url] absoluteString];照片縮略圖 [asset thumbnail]; [asset aspectRatioThumbnail];照片全尺寸圖 [[asset defaultRepresentation] fullResolutionImage];照片全屏圖 [[asset defaultRepresentation] fullScreenImage];照片創(chuàng)建時間 [asset valueForProperty:ALAssetPropertyDate];照片拍攝位置(可能為nil) [asset valueForProperty:ALAssetPropertyLocation];照片尺寸 [[asset defaultRepresentation] dimensions];
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 滦平县| 平度市| 白山市| 晋州市| 吉木萨尔县| 抚松县| 达日县| 长丰县| 错那县| 禹城市| 乌什县| 南充市| 胶州市| 连平县| 洛扎县| 莱阳市| 泗水县| 宜宾县| 牙克石市| 同仁县| 常德市| 高阳县| 溧水县| 哈密市| 宜宾市| 卢龙县| 安义县| 丹寨县| 正安县| 灌阳县| 灵寿县| 贵南县| 清远市| 竹溪县| 恩施市| 连江县| 江津市| 团风县| 渭南市| 古丈县| 千阳县|