在我們的App中 ,如何將我們從網(wǎng)絡上下載的圖片 保存到用戶設備 的系統(tǒng)相冊中,以供用戶使用呢?
以下是蘋果SDK中為我們提供的方法:在這里粗淺的講解一下大致的用法,更多的使用方法,可以查閱蘋果官方文檔,了解更多!
方法一:
++簡單保存圖片到系統(tǒng)相冊 ++
-
直接調用系統(tǒng)函數(shù)
''UIImage *image =nil;
''UIImageWriteToSavedPhotosAlbum(image,self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 必須實現(xiàn) 系統(tǒng)方法
''- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
方法二:
++在系統(tǒng)照片庫中 添加自定義相簿 并保存照片++
完整方法:
方法解析:
- 導入Photo 框架
''#import <Photos/Photos.h> - 獲取PHPhotoLibrary 對象 調用方法
''[[PHPhotoLibrary sharedPhotoLibrary] performChanges:<#^(void)changeBlock#> completionHandler:<#^(BOOL success, NSError * _Nullable error)completionHandler#>];
%% 在performChanges block中實現(xiàn)以下方法
%% completionHandler 完成后回調 block - 創(chuàng)建自定義相冊 PHAssetCollectionChangeRequest ->與相冊相關的操作類
title :相冊標題
''PHAssetCollectionChangeRequest *assetCollectionChangeRequest =[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"別有洞天"]; - 保存照片到相冊 PHAssetChangeRequest ->與照片圖片相關操作類
將照片添加到 系統(tǒng)相冊 (此次操作后,系統(tǒng)相冊中已經(jīng)保存 當前照片)
''PHAssetChangeRequest *assetChangeRequest =[PHAssetChangeRequest creationRequestForAssetFromImage:image];''
將照片從系統(tǒng)相冊中 拷貝到自定義相冊
'' PHObjectPlaceholder * objectPlaceholder=[assetChangeRequest placeholderForCreatedAsset];
%% placeholderForCreatedAsset 方法: 用來獲取一個新創(chuàng)建的asset
往自定的相冊中添加照片
''[assetCollectionChangeRequest addAssets:@[objectPlaceholder]];
寄語:
圣人千古,必有一失;余人千慮,必有一得!
本文簡述的僅僅是Photos 框架的很小一個部分,歡迎各位交流;