ALAssetsLibrary提取手机相册照片和视频
ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能。
1 _library = [[ALAssetsLibrary alloc]init]; 2 //判断当前应用是否能访问相册资源 3 /* 4 typedef NS_ENUM(NSInteger, ALAuthorizationStatus) { 5 ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候 6 ALAuthorizationStatusRestricted, 此应用程序没有被授权访问的照片数据。可能是家长控制权限。 7 8 ALAuthorizationStatusDenied, 用户已经明确否认了这一照片数据的应用程序访问. 9 ALAuthorizationStatusAuthorized 用户已授权应用访问照片数据. 10 } 11 */ 12 int author = [ALAssetsLibrary authorizationStatus]; 13 NSLog(@"author type:%d",author); 14 //关闭监听共享照片流产生的频繁通知信息 15 [ALAssetsLibrary disableSharedPhotoStreamsSupport]; 16 17 //创建一个相册到相册资源中,并通过block返回创建成功的相册ALAssetsGroup 18 [_library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) { 19 //NSString *const ALAssetsGroupPropertyName; 20 //NSString *const ALAssetsGroupPropertyType; 21 //NSString *const ALAssetsGroupPropertyPersistentID; 22 //NSString *const ALAssetsGroupPropertyURL; 23 //查看相册的名字 24 NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]); 25 //查看相册的类型 26 NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]); 27 //查看相册的存储id 28 NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]); 29 //查看相册存储的位置地址 30 NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]); 31 groupURL = [group valueForProperty:ALAssetsGroupPropertyURL]; 32 33 } failureBlock:^(NSError *error) { 34 35 }];
新添加了一个名为test的相册
1 [NSThread sleepForTimeInterval:0.5]; 2 //通过url地址在相册资源中获取该地址的资源文件ALAsset,有可能是相片或视频 3 [_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) { 4 /* 5 NSString *const ALAssetPropertyType; 6 NSString *const ALAssetPropertyLocation; 7 NSString *const ALAssetPropertyDuration; 8 NSString *const ALAssetPropertyOrientation; 9 NSString *const ALAssetPropertyDate; 10 NSString *const ALAssetPropertyRepresentations; 11 NSString *const ALAssetPropertyURLs; 12 NSString *const ALAssetPropertyAssetURL; 13 */ 14 //查看资源的地理位置信息 15 NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]); 16 //如果资源是视频,查看视频的时长 17 NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]); 18 //查看资源的方向,图片的旋转方向 19 NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]); 20 //查看资源的创建时间 21 NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]); 22 //查看资源的描述信息 23 NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]); 24 NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]); 25 //查看资源的url路径 26 NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]); 27 } failureBlock:^(NSError *error) { 28 29 }]; 30 //通过url地址获取相册资源中的一个相册 31 [_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) { 32 NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]); 33 } failureBlock:^(NSError *error) { 34 35 }]; 36 //根据选择的类型遍历相册资源中的相对应类型的所有相册,其中stop行参是指针,表示是否停止迭代,当赋值为false则停止 37 /* 38 enum { 39 ALAssetsGroupLibrary = (1 << 0), 40 ALAssetsGroupAlbum = (1 << 1), 41 ALAssetsGroupEvent = (1 << 2), 42 ALAssetsGroupFaces = (1 << 3), 43 ALAssetsGroupSavedPhotos = (1 << 4), 44 ALAssetsGroupPhotoStream = (1 << 5), 45 ALAssetsGroupAll = 0xFFFFFFFF, 46 }; 47 */ 48 [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 49 NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]); 50 } failureBlock:^(NSError *error) { 51 52 }]; 53 //保存图片到系统默认的相册中,使用nsdata的形式,并返回照片的url地址 54 [_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { 55 56 }]; 57 //保存图片到系统默认的相册中,使用cgimageref的形式,并返回照片的url地址 58 [_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { 59 60 }]; 61 62 //保存图片到系统默认的相册中,使用cgimageref的形式,并且选择图片以什么旋转方向的形式保存,并返回照片的url地址 63 /* 64 typedef enum { 65 ALAssetOrientationUp, // default orientation 66 ALAssetOrientationDown, // 180 deg rotation 67 ALAssetOrientationLeft, // 90 deg CCW 68 ALAssetOrientationRight, // 90 deg CW 69 ALAssetOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 70 ALAssetOrientationDownMirrored, // horizontal flip 71 ALAssetOrientationLeftMirrored, // vertical flip 72 ALAssetOrientationRightMirrored, // vertical flip 73 } ALAssetOrientation; 74 */ 75 UIImage* image = [UIImage imageNamed:@"test.png"]; 76 [_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) { 77 NSLog(@"save image:%@",assetURL); 78 }];
激情为梦想而生