首先介绍下iOS平台下,根据iOS不同版本执行不同方法,有很多种,这里用一种即可:
1 2 3 4 5 6 7
| NSString *version = [UIDevice currentDevice].systemVersion; if (version.doubleValue >= 8.0) { // 针对 8.0 以上的iOS系统进行处理
} else { // 针对 8.0 以下的iOS系统进行处理 }
|
对相册的操做在不同的iOS系统将会使用不同的框架,iOS8.0以下使用AssetsLibrary framework
,对于大于iOS8.0要使用Photos framework
。
检查是否有相册权限
返回当前权限状态,动态权限具体查看:
1 2 3 4 5 6 7 8 9 10 11 12
| + (int)checkPermission{ NSString *version = [UIDevice currentDevice].systemVersion; if (version.doubleValue >= 8.0) { // 针对 8.0 以上的iOS系统进行处理 PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; return (int)status; } else { // 针对 8.0 以下的iOS系统进行处理 ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; return (int)status; } }
|
申请相册权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| + (int)requestPermission { int status = [self checkPermission]; if(status == 0 || status == 2) { __block BOOL authorized = NO; dispatch_semaphore_t sema = dispatch_semaphore_create(0); NSString *version = [UIDevice currentDevice].systemVersion; if (version.doubleValue >= 8.0) { // 针对 8.0 以上的iOS系统进行处理 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { authorized = (status == PHAuthorizationStatusAuthorized); dispatch_semaphore_signal(sema); }]; } else { // 针对 8.0 以下的iOS系统进行处理 [lib enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { *stop = YES; authorized = YES; dispatch_semaphore_signal(sema); } failureBlock:^(NSError *error) { dispatch_semaphore_signal(sema); }]; } dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); return authorized ? 1 : 0; }else{ return status; } }
|
保存图片到相册
(oc)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| + (void)saveMediaNew:(NSString *)path luaID:(int) luaSaveCallback albumName:(NSString *)album{ void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *assetChangeRequest; assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:[NSURL fileURLWithPath:path]];
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
} completionHandler:^(BOOL success, NSError *error) { [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
if(success){ [self execSaveToGalleryLuaFunc: @"success" withLuaFuncID: luaSaveCallback]; } else { NSLog(@"Error creating asset: %@", error); [self execSaveToGalleryLuaFunc:[error localizedDescription] withLuaFuncID:luaSaveCallback]; } }]; };
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", album]; PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions]; if (fetchResult.count > 0) { saveBlock(fetchResult.firstObject); } else { __block PHObjectPlaceholder *albumPlaceholder; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:album]; albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection; } completionHandler:^(BOOL success, NSError *error) { if (success) { PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil]; if (fetchResult.count > 0) saveBlock(fetchResult.firstObject); else { [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; [self execSaveToGalleryLuaFunc:@"Album placeholder not found" withLuaFuncID:luaSaveCallback]; } } else { NSLog(@"Error creating album: %@", error); [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; [self execSaveToGalleryLuaFunc:[error localizedDescription] withLuaFuncID:luaSaveCallback]; } }]; } }
+ (void)saveMediaOld:(NSString *)path luaID:(int) luaSaveCallback albumName:(NSString *)album{ #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^saveBlock)(ALAssetsGroup *assetCollection) = ^void(ALAssetsGroup *assetCollection) { void (^saveResultBlock)(NSURL *assetURL, NSError *error) = ^void(NSURL *assetURL, NSError *error) { [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
if (error.code == 0) { [library assetForURL:assetURL resultBlock:^(ALAsset *asset) { [assetCollection addAsset:asset]; [self execSaveToGalleryLuaFunc: @"success" withLuaFuncID: luaSaveCallback]; } failureBlock:^(NSError* error) { NSLog(@"Error moving asset to album: %@", error); [self execSaveToGalleryLuaFunc:[error localizedDescription] withLuaFuncID:luaSaveCallback]; }]; } else { NSLog(@"Error creating asset: %@", error); [self execSaveToGalleryLuaFunc:[error localizedDescription] withLuaFuncID:luaSaveCallback]; } }; [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:saveResultBlock]; };
__block BOOL albumFound = NO; [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:album]) { *stop = YES; albumFound = YES; saveBlock(group); } else if (group == nil && albumFound==NO) { [library addAssetsGroupAlbumWithName:album resultBlock:^(ALAssetsGroup *group) { saveBlock(group); } failureBlock:^(NSError *error) { NSLog(@"Error creating album: %@", error); [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; [self execSaveToGalleryLuaFunc:[error localizedDescription] withLuaFuncID:luaSaveCallback]; }]; } } failureBlock:^(NSError* error) { NSLog(@"Error listing albums: %@", error); [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; [self execSaveToGalleryLuaFunc:[error localizedDescription] withLuaFuncID:luaSaveCallback]; }]; #endif }
|