2012-07-21 42 views

答えて

115

アプリのドキュメントフォルダにサブフォルダを作成することができます。これを行うにはNSFileManagerを使用します。

UIImagePNGRepresentationを使用して、画像をNSDataに変換してディスクに保存します。

// Create path. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"]; 

// Save image. 
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; 

コアデータは、途中でディスクに画像を保存することとは関係ありません。

+0

あなたはUIImagePNGRepresentationを使用して、すべての方位情報を失います。 –

+1

どうすれば情報を失うことなく画像を保存できますか? – Pol

3

まず、

/* create path to cache directory inside the application's Documents directory */ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"]; 

は、その後、あなたがパスは、あなたがそれを書きたいファイルの名前でファイル

NSData *photoData = UIImageJPEGRepresentation(photoImage, 1); 
[photoData writeToFile:filePath atomically:YES]; 
6
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:path atomically:YES]; 

に写真を保存する必要がありDocumentsディレクトリを取得する必要がありますに。

14

上記は役に立ちますが、サブディレクトリに保存する方法やUIImagePickerから画像を取得する方法に関する質問には答えません。 didFinishPickingMediaWithInfo:方法、であるそして、あなたはデリゲートのimagePickerControllerを実装

@interface CameraViewController() <UIImagePickerControllerDelegate> 

@end 

まず、次のような、の.mまたは.hのコードファイルのいずれかで、あなたのコントローラは、画像ピッカーのデリゲートを実装することを指定する必要があります。あなたは、イメージピッカーから写真を取得し、(もちろん、あなたが貯蓄を扱う別のクラス/オブジェクトを持っているかもしれないが、私はちょうどメソッド内のコードを紹介します)、それを保存することができます。ここで

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    // get the captured image 
    UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage]; 


    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"]; 

    NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"]; 

    // Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec 
    NSData *imageData = UIImagePNGRepresentation(image); 
    [imageData writeToFile:filePath atomically:YES]; 
} 

場合あなたは、JPEG画像、ラST 3行は次のようになります。

スウィフト3では
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"]; 

// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec 
NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85% 
[imageData writeToFile:filePath atomically:YES]; 
25

// Create path. 
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
let filePath = "\(paths[0])/MyImageName.png" 

// Save image. 
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true) 
+1

もう有効ではありません。あなたは '.write()throws'を使う必要があります –

9
extension UIImage { 
    /// Save PNG in the Documents directory 
    func save(_ name: String) { 
     let path: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! 
     let url = URL(fileURLWithPath: path).appendingPathComponent(name) 
     try! UIImagePNGRepresentation(self)?.write(to: url) 
     print("saved image at \(url)") 
    } 
} 

// Usage: Saves file in the Documents directory 
image.save("climate_model_2017.png") 
関連する問題