沙盒路徑獲取以及圖片保存到相簿的方法
iphone沙盒(sandbox)中的幾個目錄獲取方式:
// 獲取沙盒主目錄路徑 NSString *homeDir = NSHomeDirectory(); // 獲取Documents目錄路徑 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; // 獲取Caches目錄路徑 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDir = [paths objectAtIndex:0]; // 獲取tmp目錄路徑 NSString *tmpDir = NSTemporaryDirectory(); // 獲取當(dāng)前程序包中一個圖片資源(apple.png)路徑 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"]; UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
保存圖片到相冊的三種方法:
1、使用UIImageWriteToSavedPhotosAlbum函數(shù)將圖片保存到相冊,如:
- (void)loadImageFinished:(UIImage *)image { UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo); }
第一個參數(shù)是要保存到相冊的圖片對象
第二個參數(shù)是保存完成后回調(diào)的目標(biāo)對象
第三個參數(shù)就是保存完成后回調(diào)到目標(biāo)對象的哪個方法中,方法的聲明要如代碼中所示的:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
第四個參數(shù)在保存完成后,會原封不動地傳回到回調(diào)方法的contextInfo參數(shù)中。
2、使用AssetsLibrary框架中的ALAssetsLibrary類來實(shí)現(xiàn)。具體代碼如下:
- (void)loadImageFinished:(UIImage *)image { __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"assetURL = %@, error = %@", assetURL, error); lib = nil; }]; }
使用了ALAssetsLibrary類的writeImageToSavedPhotosAlbum:metadata:completionBlock:方法實(shí)現(xiàn)。其中第一個參數(shù)是一個CGImageRef的對象,表示要傳入的圖片。第二個參數(shù)是圖片的一些屬性,這里沒有設(shè)置所以傳入nil。最后一個completionBlock是保存完成后的回調(diào),在這個回調(diào)中可以取到保存后的圖片路徑以及保存失敗時的錯誤信息。
注意:使用該類時需要導(dǎo)入AssetsLibrary.framework。而且該類需要在iOS4.0以上可以使用,但是在iOS9.0之后就被標(biāo)記為過時方法。官方建議使用Photos.framework中的PHPhotoLibrary進(jìn)行代替,也就是下面所說的第三種方法。
3、使用Photos框架的PHPhotoLibrary類來實(shí)現(xiàn)保存到相冊功能。代碼如下:
- (void)loadImageFinished:(UIImage *)image { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ /寫入圖片到相冊 PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; } completionHandler:^(BOOL success, NSError * _Nullable error) { NSLog(@"success = %d, error = %@", success, error); }]; }
該例子中先調(diào)用PHPhotoLibrary類的performChanges:completionHandler:方法,然后在它的changeBlock中,通過PHAssetChangeRequest類的creationRequestForAssetFromImage:方法傳入一個圖片對象即可實(shí)現(xiàn)保存到相冊的功能。然后completionHandler中會告訴我們是否操作成功。
也許會有人需要在保存相冊后得到圖片的PHAsset對象來進(jìn)行后續(xù)操作(昨天剛好碰到有朋友遇到這樣的問題)。那么,這里對上面例子進(jìn)行改進(jìn),在創(chuàng)建PHAssetChangeRequest后將它的placeholderForCreatedAsset屬性的localIdentifier屬性保存到一個數(shù)組中,等待操作完成后再通過這個數(shù)組來查找剛剛添加的圖片對象。
請看下面栗子:
- (void)loadImageFinished:(UIImage *)image { NSMutableArray *imageIds = [NSMutableArray array]; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ //寫入圖片到相冊 PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; //記錄本地標(biāo)識,等待完成后取到相冊中的圖片對象 [imageIds addObject:req.placeholderForCreatedAsset.localIdentifier]; } completionHandler:^(BOOL success, NSError * _Nullable error) { NSLog(@"success = %d, error = %@", success, error); if (success) { //成功后取相冊中的圖片對象 __block PHAsset *imageAsset = nil; PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:imageIds options:nil]; [result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { imageAsset = obj; *stop = YES; }]; if (imageAsset) { //加載圖片數(shù)據(jù) [[PHImageManager defaultManager] requestImageDataForAsset:imageAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) { NSLog("imageData = %@", imageData); }]; } } }]; }
以上這篇沙盒路徑獲取以及圖片保存到相簿的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決iOS11圖片下拉放大出現(xiàn)信號欄白條的bug問題
這篇文章主要介紹了iOS11圖片下拉放大出現(xiàn)信號欄白條的bug問題,需要的朋友參考下吧2017-09-09React Native學(xué)習(xí)教程之Modal控件自定義彈出View詳解
這篇文章主要給大家介紹了關(guān)于React Native學(xué)習(xí)教程之Modal控件自定義彈出View的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用React Native具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10iOS實(shí)現(xiàn)手指點(diǎn)擊出現(xiàn)波紋的效果
最近在閑暇的時間做了一個反饋手指點(diǎn)擊屏幕的效果,用到了CAShapeLayer和基本的動畫知識,實(shí)現(xiàn)的效果很贊,這種效果使用在某些頁面上肯定會給用戶更有趣的體驗(yàn),特別是面向兒童的app中。文中給出了詳細(xì)的示例代碼,感興趣的朋友們下面來一起看看吧。2016-12-12