不知道你們有沒有這樣的僥幸心理:Apple更新了新的框架API,舊的雖然被標為廢棄了但還是能用眶诈,就一直用著舊的偷懶不去改最新的涨醋。有句話說得好:不是不報,時候未到册养。我最近遇到了要報的懶东帅。
在iOS里,保存圖片到相冊有一個簡單的UIImage
的方法:
UIImageWriteToSavedPhotosAlbum(_image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
然后需要實現(xiàn)上面SEL里的回調球拦,一個系統(tǒng)的方法:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存失敗");
} else {
NSLog(@"保存成功");
}
}
當然這個只能簡單的保存圖片到默認的系統(tǒng)相冊中靠闭,如果要創(chuàng)建自己的相冊,就需要用ALAssetsLibrary
框架:
// 先保存圖片
- (void)saveImage:(UIImage*)image toAlbum:(NSString *)albumName{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
[assetsLibrary writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Save image fail:%@",error);
}else{
NSLog(@"Save image succeed.");
[self addAssetURL:assetURL toAlbum:albumName withAssestLibrary:assetsLibrary completionBlock:(SaveRecordCompletion)completionBlock failBlock:(void(^)(NSError *error))failBlock] ;
}
}];
// 再添加到指定相冊
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withAssestLibrary:(ALAssetsLibrary *)assetsLibrary completionBlock:(SaveRecordCompletion)completionBlock failBlock:(void(^)(NSError *error))failBlock
{
__block BOOL albumWasFound = NO;
//遍歷所有的相冊
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//compare the names of the albums
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]] == NSOrderedSame)
{
//找到名字為albumName的相冊
albumWasFound = YES;
//get a hold of the photo's asset instance
[assetsLibrary assetForURL: assetURL resultBlock:^(ALAsset *asset) {
//add photo to the target album
//NSAssert(asset != nil, @"asset不能為空");
[group addAsset: asset];
} failureBlock: nil];
*stop = YES;
}
if (group==nil && albumWasFound==NO)
{
__weak ALAssetsLibrary* weakAssetsLibrary = assetsLibrary;
[self addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group) {
[weakAssetsLibrary assetForURL: assetURL resultBlock:^(ALAsset *asset)
{
NSAssert(asset != nil, @"asset不能為空");
//add photo to the newly created album
[group addAsset: asset];
} failureBlock: nil];
} failureBlock: nil];
*stop = YES;
}
} failureBlock:failBlock];
}
因為ALAssetsLibrary的API在iOS9之后被標位棄用了坎炼,所以會有出錯的風險愧膀。我遇到的問題是,當開啟使用iCloud圖庫時谣光,圖片只會保存在系統(tǒng)默認的相冊下檩淋,保存不到我們應用的相冊下,關閉iCloud時就不會有這種問題(在iOS10、11下出現(xiàn)的)蟀悦。所以懷疑到應該是在開啟iCloud時媚朦,蘋果做了一些操作導致舊的框架不支持了。
下面主要看下在iOS8后出現(xiàn)的Photos
框架的簡單使用日戈。
首先询张,我們可以創(chuàng)建一個helpe
類,用來單獨做對相冊的操作(比如我的叫SRHPhotoHelper
浙炼,當然你可以按照自己的想法隨意寫)份氧。
直接上代碼:
+ (void)srh_saveImage:(UIImage *)image completionHandle:(void (^)(NSError *, NSString *))completionHandler {
// 1. 獲取照片庫對象
PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
// 假如外面需要這個 localIdentifier ,可以通過block傳出去
__block NSString *localIdentifier = @"";
// 2. 調用changeblock
[library performChanges:^{
// 2.1 創(chuàng)建一個相冊變動請求
PHAssetCollectionChangeRequest *collectionRequest = [self getCurrentPhotoCollectionWithAlbumName:SRHAlbumName];
// 2.2 根據(jù)傳入的照片弯屈,創(chuàng)建照片變動請求
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// 2.3 創(chuàng)建一個占位對象
PHObjectPlaceholder *placeholder = [assetRequest placeholderForCreatedAsset];
localIdentifier = placeholder.localIdentifier;
// 2.4 將占位對象添加到相冊請求中
[collectionRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
[iConsole log:@"保存照片出錯>>>%@", [error description]];
completionHandler(error, nil);
} else {
completionHandler(nil, localIdentifier);
}
}];
}
+ (PHAssetCollectionChangeRequest *)getCurrentPhotoCollectionWithAlbumName:(NSString *)albumName {
// 1. 創(chuàng)建搜索集合
PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 2. 遍歷搜索集合并取出對應的相冊蜗帜,返回當前的相冊changeRequest
for (PHAssetCollection *assetCollection in result) {
if ([assetCollection.localizedTitle containsString:albumName]) {
PHAssetCollectionChangeRequest *collectionRuquest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
return collectionRuquest;
}
}
// 3. 如果不存在,創(chuàng)建一個名字為albumName的相冊changeRequest
PHAssetCollectionChangeRequest *collectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
return collectionRequest;
}
具體的講解可以自己去查API或者看一下這篇文章资厉、這篇文章厅缺。后續(xù)我會把取相冊圖片、視頻的代碼分享出來酌住,包括完整的demo店归。