ALAssetsLibrary類是表明系統中整個資源庫,使用它能夠訪問資源庫中的資源和保存照片,視頻等功能。url
_library = [[ALAssetsLibrary alloc]init];
//判斷當前應用是否能訪問相冊資源
/*
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, 用戶還沒有作出了選擇這個應用程序的問候
ALAuthorizationStatusRestricted, 此應用程序沒有被受權訪問的照片數據。多是家長控制權限。
ALAuthorizationStatusDenied, 用戶已經明確否定了這一照片數據的應用程序訪問.
ALAuthorizationStatusAuthorized 用戶已受權應用訪問照片數據.
}
*/
int author = [ALAssetsLibrary authorizationStatus];
NSLog(@"author type:%d",author);
//關閉監聽共享照片流產生的頻繁通知信息
[ALAssetsLibrary disableSharedPhotoStreamsSupport];
//建立一個相冊到相冊資源中,並經過block返回建立成功的相冊ALAssetsGroup
[_library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
//NSString *const ALAssetsGroupPropertyName;
//NSString *const ALAssetsGroupPropertyType;
//NSString *const ALAssetsGroupPropertyPersistentID;
//NSString *const ALAssetsGroupPropertyURL;
//查看相冊的名字
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
//查看相冊的類型
NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
//查看相冊的存儲id
NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
//查看相冊存儲的位置地址
NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];
} failureBlock:^(NSError *error) {
}];spa
新添加了一個名爲test的相冊
指針
[NSThread sleepForTimeInterval:0.5];
//經過url地址在相冊資源中獲取該地址的資源文件ALAsset,有多是相片或視頻
[_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {
/*
NSString *const ALAssetPropertyType;
NSString *const ALAssetPropertyLocation;
NSString *const ALAssetPropertyDuration;
NSString *const ALAssetPropertyOrientation;
NSString *const ALAssetPropertyDate;
NSString *const ALAssetPropertyRepresentations;
NSString *const ALAssetPropertyURLs;
NSString *const ALAssetPropertyAssetURL;
*/
//查看資源的地理位置信息
NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
//若是資源是視頻,查看視頻的時長
NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
//查看資源的方向,圖片的旋轉方向
NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
//查看資源的建立時間
NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
//查看資源的描述信息
NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
//查看資源的url路徑
NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
} failureBlock:^(NSError *error) {
}];
//經過url地址獲取相冊資源中的一個相冊
[_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {
}];
//根據選擇的類型遍歷相冊資源中的相對應類型的全部相冊,其中stop行參是指針,表示是否中止迭代,當賦值爲false則中止
/*
enum {
ALAssetsGroupLibrary = (1 << 0),
ALAssetsGroupAlbum = (1 << 1),
ALAssetsGroupEvent = (1 << 2),
ALAssetsGroupFaces = (1 << 3),
ALAssetsGroupSavedPhotos = (1 << 4),
ALAssetsGroupPhotoStream = (1 << 5),
ALAssetsGroupAll = 0xFFFFFFFF,
};
*/
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {
}];
//保存圖片到系統默認的相冊中,使用nsdata的形式,並返回照片的url地址
[_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
}];
//保存圖片到系統默認的相冊中,使用cgimageref的形式,並返回照片的url地址
[_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
}];
//保存圖片到系統默認的相冊中,使用cgimageref的形式,而且選擇圖片以什麼旋轉方向的形式保存,並返回照片的url地址
/*
typedef enum {
ALAssetOrientationUp, // default orientation
ALAssetOrientationDown, // 180 deg rotation
ALAssetOrientationLeft, // 90 deg CCW
ALAssetOrientationRight, // 90 deg CW
ALAssetOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
ALAssetOrientationDownMirrored, // horizontal flip
ALAssetOrientationLeftMirrored, // vertical flip
ALAssetOrientationRightMirrored, // vertical flip
} ALAssetOrientation;
*/
UIImage* image = [UIImage imageNamed:@"test.png"];
[_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"save image:%@",assetURL);
}];
視頻