項目這次要降到7.0,
先看看里面的餡...

ALAsset 用戶照片庫中一個單獨的資源,簡單而言就是單張圖片或者視頻的元數(shù)據(jù)吧 對應(yīng)于PHAsset
ALAssetsFilter 類型過濾器 給 ALAssetsGroup這個集合類過濾得出相應(yīng)的產(chǎn)物
如:[group setAssetsFilter:[ALAssetsFilter allPhotos]];//僅篩選出圖片 allVideos allAssets
ALAssetsGroup 這是個資源的集合類型,可包含各種媒體類型 可同上面的過濾器過濾出自己想要的
ALAssetsLibrary 依靠這個類我們才能開始訪問到系統(tǒng)相冊 (不同于PHImageManager它不是單例...)
可根據(jù)groupURL、assetURl、groupType、groupName等條件獲取到對應(yīng)的產(chǎn)物,也可以使用該類將圖片或者video寫到本地中 ,當(dāng)然也可以根據(jù)該類查詢當(dāng)前app獲得相冊權(quán)限的情況。
ALAssetsRepresentation ALAsset都有一個ALAssetsRepresentation類型的屬性,我們可以在此獲取ALAsset更加詳細(xì)的信息

練練手吧
首先我要獲得相冊中所有的圖片
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group setAssetsFilter:[ALAssetsFilter allVideos]];
// group.numberOfAssets//可查詢該group中一共有多少個資源
// 其實最好是打印group出來 可看到該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ù) 這里的是過濾后得到的全是圖片
ALAssetRepresentation *representation = [result defaultRepresentation];
NSURL *URL = [representation url];//此URL并非絕對路徑...是相冊中的相對路徑,使用一定要注意,一旦library釋放了,改url也就無效了,你另外作保存也沒用...如果該資源是video 那么該URL可用于MPMoviePlayerViewController 播放視頻
/*URL: assets-library://asset/asset.PNG?id=AAD73085-DE1D-4BD3-837A-09DE797A4C2A&ext=PNG*/
NSDate *imageDate = [result valueForProperty:ALAssetPropertyDate];
//無論你原圖多大 縮略圖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);
}];
來看看上面的參數(shù)情況
第一個枚舉器會回調(diào)兩次,第一次為膠卷的group,第二次為nil (PS:枚舉器到nil才會停止,像數(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個枚舉類型,應(yīng)該不會添加更多了,因為<AssetsLibrary/AssetsLibrary.h>都廢棄掉了
這里我選擇了自定義范圍以及看而自定義枚舉方式的一個枚舉器
NSIndexSet:范圍
options:可選擇并發(fā)或者是反向枚舉,方然也可以 |
這里的block 提供了3個參數(shù) 資源元數(shù)據(jù) 資源角標(biāo) 以及可自定義控制停止枚舉
然后圖片資源我是有了,我怎么訪問圖片呢?根據(jù)URL???
可是因為該URL并非絕對路徑,一旦我的ALAssetsLibrary *library 釋放掉了 這個URL也就失效了,如果我把它弄成單例,但時刻也只能有一個圖片或者是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)部提供了方法
模擬器測試的時候有出現(xiàn)過//有時representation.size = nil 的情況,可能是video添加到相冊時發(fā)生了謀種錯誤吧
個人認(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;
}```