項(xiàng)目這次要降到7.0洞辣,
先看看里面的餡...
ALAsset 用戶照片庫(kù)中一個(gè)單獨(dú)的資源扬霜,簡(jiǎn)單而言就是單張圖片或者視頻的元數(shù)據(jù)吧 對(duì)應(yīng)于PHAsset
ALAssetsFilter 類型過(guò)濾器 給 ALAssetsGroup這個(gè)集合類過(guò)濾得出相應(yīng)的產(chǎn)物
如:[group setAssetsFilter:[ALAssetsFilter allPhotos]];//僅篩選出圖片 allVideos allAssets
ALAssetsGroup 這是個(gè)資源的集合類型而涉,可包含各種媒體類型 可同上面的過(guò)濾器過(guò)濾出自己想要的
ALAssetsLibrary 依靠這個(gè)類我們才能開(kāi)始訪問(wèn)到系統(tǒng)相冊(cè) (不同于PHImageManager它不是單例...)
可根據(jù)groupURL、assetURl材原、groupType季眷、groupName等條件獲取到對(duì)應(yīng)的產(chǎn)物瘟裸,也可以使用該類將圖片或者video寫到本地中 ,當(dāng)然也可以根據(jù)該類查詢當(dāng)前app獲得相冊(cè)權(quán)限的情況诵竭。
ALAssetsRepresentation ALAsset都有一個(gè)ALAssetsRepresentation類型的屬性,我們可以在此獲取ALAsset更加詳細(xì)的信息
練練手吧
首先我要獲得相冊(cè)中所有的圖片
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
// group.numberOfAssets//可查詢?cè)揼roup中一共有多少個(gè)資源
// 其實(shí)最好是打印group出來(lái) 可看到該group的名字
if (group.numberOfAssets > 0) {
NSIndexSet *IndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, group.numberOfAssets)];//這里影響group枚舉的位置由 Range 決定
[group enumerateAssetsAtIndexes:IndexSet options:NSEnumerationReverse/*枚舉方向*/ usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//resoult 資源元數(shù)據(jù) 這里的是過(guò)濾后得到的全是圖片
ALAssetRepresentation *representation = [result defaultRepresentation];
NSURL *URL = [representation url];//此URL并非絕對(duì)路徑...是相冊(cè)中的相對(duì)路徑,使用一定要注意病线,一旦library釋放了鲤嫡,改url也就無(wú)效了,你另外作保存也沒(méi)用...如果該資源是video 那么該URL可用于MPMoviePlayerViewController 播放視頻
/*URL: assets-library://asset/asset.PNG?id=AAD73085-DE1D-4BD3-837A-09DE797A4C2A&ext=PNG*/
NSDate *imageDate = [result valueForProperty:ALAssetPropertyDate];
//無(wú)論你原圖多大 縮略圖size都是{125惕耕,125}
UIImage *thumbnail = [UIImage imageWithCGImage:result.thumbnail];
//屏幕分辨率大小的圖 原圖分辨率小于屏幕的則按原圖分辨率 (按比例進(jìn)行縮減)
UIImage *fullScreenImage = [UIImage imageWithCGImage:[representation fullScreenImage]];
//原圖
UIImage *fullResolutionImage = [UIImage imageWithCGImage:[representation fullResolutionImage]];
}];
}];
// *stop = YES;//之前我是忘了怎么停止枚舉器的 - -///
}
} failureBlock:^(NSError *error) {
NSLog(@"error : %@",error.description);
}];
來(lái)看看上面的參數(shù)情況
第一個(gè)枚舉器會(huì)回調(diào)兩次司澎,第一次為膠卷的group,第二次為nil (PS:枚舉器到nil才會(huì)停止挤安,像數(shù)組那樣) ```
>ALAssetsGroupType:
ALAssetsGroupLibrary // The Library group that includes all assets.
ALAssetsGroupAlbum // All the albums synced from iTunes or created on the device.
ALAssetsGroupEvent // All the events synced from iTunes.
ALAssetsGroupFaces // All the faces albums synced from iTunes.
ALAssetsGroupSavedPhotos // The Saved Photos album.
ALAssetsGroupPhotoStream // The PhotoStream album.
ALAssetsGroupAll // The same as ORing together all the available group types,
而ALAssetsGroup *group 有3個(gè)枚舉類型,應(yīng)該不會(huì)添加更多了蛤铜,因?yàn)?lt;AssetsLibrary/AssetsLibrary.h>都廢棄掉了
這里我選擇了自定義范圍以及看而自定義枚舉方式的一個(gè)枚舉器
NSIndexSet:范圍
options:可選擇并發(fā)或者是反向枚舉昂羡,方然也可以 |
這里的block 提供了3個(gè)參數(shù) 資源元數(shù)據(jù) 資源角標(biāo) 以及可自定義控制停止枚舉
然后圖片資源我是有了,我怎么訪問(wèn)圖片呢虐先?根據(jù)URL蛹批??腐芍?
可是因?yàn)樵揢RL并非絕對(duì)路徑猪勇,一旦我的ALAssetsLibrary *library 釋放掉了 這個(gè)URL也就失效了,如果我把它弄成單例泣刹,但時(shí)刻也只能有一個(gè)圖片或者是video的URL椅您,并不滿足我的需求。
顯然雪隧,保存到本地沙盒是目前的比較好的處理方法员舵,但是但是但是!一旦我圖片保存得多庄拇,我內(nèi)存不也一樣爆咋巫玻。祠汇。熄诡。這這這凰浮。。菜拓。所以我們保存的圖片的像素要自己把握了笛厦。。贱鄙。
//寫入tmp
- (NSString *)getPathWhileCreatingFlieByAppendingPathComponent:(NSString *)str {
NSFileManager *manager = [NSFileManager defaultManager];
NSString *tmpPath = NSTemporaryDirectory();
NSString *dirPath = [tmpPath stringByAppendingPathComponent:@"NameOfFolder"];
if (![manager fileExistsAtPath:dirPath]) {
[manager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *imagePath = [dirPath stringByAppendingPathComponent:str];
if (imagePath) {
return imagePath;
}
return nil;
}```
需求是video的話姨谷,video也可以保存到本地中梦湘,而且該框架內(nèi)部提供了方法
模擬器測(cè)試的時(shí)候有出現(xiàn)過(guò)//有時(shí)representation.size = nil 的情況,可能是video添加到相冊(cè)時(shí)發(fā)生了謀種錯(cuò)誤吧
個(gè)人認(rèn)為在外部檢查representation.size哼拔,處理representation.size = nil的情況禁灼,以及控制要寫文件大小
- (NSString *)getVideoPath:(ALAssetRepresentation *)representation{
Byte *buffer = (Byte*)malloc(representation.size);
NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:representation.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
NSString *savingPath = [NSTemporaryDirectory() stringByAppendingFormat:@"%@",@"customVideoName"];//representation.fileName
[data writeToFile:savingPath atomically:YES];
return savingPath;
}```