1. 程式人生 > >iOS 常用的儲存圖片的兩個方法

iOS 常用的儲存圖片的兩個方法

第一種、就是儲存到系統相簿中,這個也是最簡單的,當然使用者想要訪問系統相簿也是需要許可權的,需要使用者同意。

在Plist檔案加入下面的鍵值對就可以了

key : Privacy - Photo Library Usage Description          value:字串即可(例如:需要使用手機相簿)

加入完成以後就是儲存圖片的方法

#pragma mark -- <儲存到相簿>

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

      NSString *msg = nil ;

      if(error){

            msg = @"儲存圖片失敗" ;

     }else{

            msg = @"儲存圖片成功" ;

      }

}

接下來就是呼叫程式碼,呼叫下面的方法,就必須實現上面的方法,否則會出現APP閃退

 //引數1:圖片物件

 //引數2:成功方法繫結的target

 //引數3:成功後呼叫方法

 //引數4:需要傳遞資訊(成功後呼叫方法的引數)

//沒有特殊操作,直接講下程式碼粘到專案中即可

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

第二種、也是比較常用的,就是見圖片儲存到沙盒路徑下,通常都要儲存到Documents下,實現程式碼如下

/**

 將圖片儲存到沙盒目錄下儲存成jpg形式,可以將圖片儲存成不通的格式型別、png、jpg等 ,可自行設定

 @param image 圖片

 @param imageName 圖片名字

 @param extension 字尾,png,jpg

 @param directoryPath 沙盒路徑

 */

- (void) saveImage:(UIImage *)image withFileName:(NSString *)imageName  ofType:(NSString *)extension inDirectory:(NSString *)directoryPath{

    NSData *data  = UIImageJPEGRepresentation(image,.000000005);

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *createPath = [NSString stringWithFormat:@"%@/BlueCamMyImage/%@", directoryPath,name];

    // 判斷資料夾是否存在,如果不存在,則建立

    if (![[NSFileManager defaultManager]fileExistsAtPath:createPath]) {

        //如果沒有就建立這個 想建立的資料夾   ()

        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];

        //然後儲存沙盒路徑下Documents資料夾下的BlueCamMyImage(可隨意定義)資料夾中,

        NSString * DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BlueCamMyImage/%@",name]];

        NSString *imgFileName = [NSString stringWithFormat:@"/%@.%@",imageName,extension];

        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:imgFileName]contents:data attributes:nil];

    } else {

        //資料夾存在   直接儲存 沙盒路徑下Documents資料夾下的BlueCamMyImage(可隨意定義)資料夾中,

        NSString * DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BlueCamMyImage/%@",name]];

        NSString *imgFileName = [NSString stringWithFormat:@"/%@.%@",imageName,extension];

        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:imgFileName]contents:data attributes:nil];

    }

}

如果很多類使用,可以將此程式碼封裝成工具,下面是呼叫

[self saveImage:@“圖片的image物件” withFileName:@“圖片名字,可隨意定義”  ofType:@"png(圖片字尾)" inDirectory:@“沙盒路徑(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject)”];

如何將網路圖片轉成image呢,下面程式碼實現

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@""]]];  

如何獲取螢幕截圖image呢,程式碼實現:

    UIGraphicsBeginImageContext(self.view.bounds.size);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NSLog(@"image:%@",image); //拿到image