AssetsLibrary使用介绍
生活随笔
收集整理的這篇文章主要介紹了
AssetsLibrary使用介绍
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、概述??? AssetsLibrary框架:從iOS4.0開始提供,可以通過它獲取設(shè)備里的圖片和視頻等資料,以API的方式提供,而且界面式的,比UIImagePickerViewController靈活很多。既可以讀又可以寫。
二、讀取資料庫(kù)中的全部資料
1.大概步驟,首先通過ALAssetsLibrary獲取group,然后再獲取每個(gè)group中的每個(gè)ALAsset,通過ALAsset獲取圖片或者視頻鏈接。
2.獲取全部資料(ALAsset)以及鏈接:
? NSMutableArray *assetGroups = [[NSMutableArray alloc] init];? void (^assetGroupEnumerator) (struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop{? ? if(group != nil) {? ? ? ?[assetGroups addObject:group];? ? }? };?? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];? NSUInteger groupTypes = ALAssetsGroupAll;?? [library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:nil];? [library release]; ? NSMutableArray *assets = [[NSMutableArray alloc] init];? NSMutableArray *assetURLArray = [[NSMutableArray alloc] init];? void (^assetEnumerator) (struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {? ? if(result != nil) {? ? ? ?if(![assetURLArray containsObject:[result valueForProperty:ALAssetPropertyURLs]]) {? ? ? ? ?if(![[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {? ? ? ? ? ?[assetURLArray addObject:[result valueForProperty:ALAssetPropertyURLs]];? ? ? ? ? ?[assets addObject:result];? ? ? ? ?}? ? ? ?}? ? }? };?? for (ALAssetsGroup *group in assetGroups) {? ? ?[group enumerateAssetsUsingBlock:assetEnumerator];? }??3.通過ALAsset鏈接獲取圖片或者視頻內(nèi)容? ??? ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *returnAsset){ ??? ? ?//獲取資料的縮略圖,圖片視頻通用? ? ?UIImage *image = [UIImage imageWithCGImage:returnAsset.defaultRepresentation.fullResolutionImage];? ? ?? ? ?//針對(duì)圖片或者視頻做自定義處理? ? ?NSString *type = [returnAsset valueForProperty:ALAssetPropertyType];? ? ?if([type isEqualToString:ALAssetTypePhoto]){? ? ? ? ? ? ? ?}else if([type isEqualToString:ALAssetTypeVideo]){? ? ? //獲取視頻時(shí)長(zhǎng)? ? ? ? NSNumber *duration = [returnAsset valueForProperty:ALAssetPropertyDuration]? ? ?} ?? };? ??? NSString *assetURL = [assetURLArray objectAtIndex:0];? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];? [library assetForURL:[NSURL URLWithString:assetURL] resultBlock:resultblock failureBlock:nil];? [library release];??4.有時(shí)候有些視頻資料沒有寫入資料庫(kù),但是我們?nèi)匀恍枰@取視頻的時(shí)長(zhǎng)。這時(shí)候我們就不能用第3條中提到的API來獲取視頻長(zhǎng)度,我們可以采用AVFoundation框架中的一個(gè)類:AVURLAsset。? 代碼如下:? AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];? CMTime ctTime = asset.duration;? [asset release];? NSInteger totalSeconds = ctTime.value / ctTime.timescale;
三、將本地圖片或者視頻寫入資料庫(kù)
1.步驟:調(diào)用ALAssetsLibrary將視頻或者圖片寫入資料庫(kù),寫入成功之后,返回一個(gè)URL鏈接。
2.代碼:? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];? if([cacheFilePath rangeOfString:@"jpg" options:NSCaseInsensitiveSearch].length > 0 ||? ? ?[cacheFilePath rangeOfString:@"png" options:NSCaseInsensitiveSearch].length > 0)? {? ? ?UIImage *image = [UIImage imageWithContentsOfFile:cacheFilePath];? ? ?[library writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error){? ? ? ? NSString *assetUrlString = assetURL.absoluteString;? ? ? ? //根據(jù)需求做相應(yīng)動(dòng)作,比如保存這個(gè)鏈接? ? ?}];? }else{? ? ?NSURL *cacheURL = [NSURL URLWithString:cacheFilePath];? ? ?[library writeVideoAtPathToSavedPhotosAlbum:cacheURL completionBlock:^(NSURL *assetURL, NSError *error){? ? ? ? NSString *assetUrlString = assetURL.absoluteString;? ? ? ? //根據(jù)需求做相應(yīng)動(dòng)作,比如保存這個(gè)鏈接? ? ?}];? }? [library release];
二、讀取資料庫(kù)中的全部資料
1.大概步驟,首先通過ALAssetsLibrary獲取group,然后再獲取每個(gè)group中的每個(gè)ALAsset,通過ALAsset獲取圖片或者視頻鏈接。
2.獲取全部資料(ALAsset)以及鏈接:
? NSMutableArray *assetGroups = [[NSMutableArray alloc] init];? void (^assetGroupEnumerator) (struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop{? ? if(group != nil) {? ? ? ?[assetGroups addObject:group];? ? }? };?? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];? NSUInteger groupTypes = ALAssetsGroupAll;?? [library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:nil];? [library release]; ? NSMutableArray *assets = [[NSMutableArray alloc] init];? NSMutableArray *assetURLArray = [[NSMutableArray alloc] init];? void (^assetEnumerator) (struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {? ? if(result != nil) {? ? ? ?if(![assetURLArray containsObject:[result valueForProperty:ALAssetPropertyURLs]]) {? ? ? ? ?if(![[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {? ? ? ? ? ?[assetURLArray addObject:[result valueForProperty:ALAssetPropertyURLs]];? ? ? ? ? ?[assets addObject:result];? ? ? ? ?}? ? ? ?}? ? }? };?? for (ALAssetsGroup *group in assetGroups) {? ? ?[group enumerateAssetsUsingBlock:assetEnumerator];? }??3.通過ALAsset鏈接獲取圖片或者視頻內(nèi)容? ??? ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *returnAsset){ ??? ? ?//獲取資料的縮略圖,圖片視頻通用? ? ?UIImage *image = [UIImage imageWithCGImage:returnAsset.defaultRepresentation.fullResolutionImage];? ? ?? ? ?//針對(duì)圖片或者視頻做自定義處理? ? ?NSString *type = [returnAsset valueForProperty:ALAssetPropertyType];? ? ?if([type isEqualToString:ALAssetTypePhoto]){? ? ? ? ? ? ? ?}else if([type isEqualToString:ALAssetTypeVideo]){? ? ? //獲取視頻時(shí)長(zhǎng)? ? ? ? NSNumber *duration = [returnAsset valueForProperty:ALAssetPropertyDuration]? ? ?} ?? };? ??? NSString *assetURL = [assetURLArray objectAtIndex:0];? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];? [library assetForURL:[NSURL URLWithString:assetURL] resultBlock:resultblock failureBlock:nil];? [library release];??4.有時(shí)候有些視頻資料沒有寫入資料庫(kù),但是我們?nèi)匀恍枰@取視頻的時(shí)長(zhǎng)。這時(shí)候我們就不能用第3條中提到的API來獲取視頻長(zhǎng)度,我們可以采用AVFoundation框架中的一個(gè)類:AVURLAsset。? 代碼如下:? AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];? CMTime ctTime = asset.duration;? [asset release];? NSInteger totalSeconds = ctTime.value / ctTime.timescale;
三、將本地圖片或者視頻寫入資料庫(kù)
1.步驟:調(diào)用ALAssetsLibrary將視頻或者圖片寫入資料庫(kù),寫入成功之后,返回一個(gè)URL鏈接。
2.代碼:? ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];? if([cacheFilePath rangeOfString:@"jpg" options:NSCaseInsensitiveSearch].length > 0 ||? ? ?[cacheFilePath rangeOfString:@"png" options:NSCaseInsensitiveSearch].length > 0)? {? ? ?UIImage *image = [UIImage imageWithContentsOfFile:cacheFilePath];? ? ?[library writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error){? ? ? ? NSString *assetUrlString = assetURL.absoluteString;? ? ? ? //根據(jù)需求做相應(yīng)動(dòng)作,比如保存這個(gè)鏈接? ? ?}];? }else{? ? ?NSURL *cacheURL = [NSURL URLWithString:cacheFilePath];? ? ?[library writeVideoAtPathToSavedPhotosAlbum:cacheURL completionBlock:^(NSURL *assetURL, NSError *error){? ? ? ? NSString *assetUrlString = assetURL.absoluteString;? ? ? ? //根據(jù)需求做相應(yīng)動(dòng)作,比如保存這個(gè)鏈接? ? ?}];? }? [library release];
總結(jié)
以上是生活随笔為你收集整理的AssetsLibrary使用介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux脚本实现红绿灯,javascr
- 下一篇: Base64编码解码原理