2011-02-10 133 views
19

我有一個iPhone應用程序使用UIImagePickerController。作爲sourceTypeiPhone - UIImagePickerController - >將圖像保存到應用程序文件夾

  • UIImagePickerControllerSourceTypePhotoLibrary
  • UIImagePickerControllerSourceTypeCamera
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum

,以便用戶可以進行照片或選擇一個從照片或相機上的照片庫。

圖像將顯示在UIImageView中。如果用戶關閉應用程序,應該保存圖像。所以對於文本字段,我使用NSUserDefaults。我知道,使用NSData將圖像保存在NSUSerDefaults中並不是一種好方法,所以我想將圖像保存/複製到由我的應用程序控制的文件夾中,類似於NSUserDefaults。

我該怎麼做?我想保存它,然後我可以將文件的路徑寫入我的NSUserDefaults,並在應用程序啓動時讀取它。

謝謝你提前&最好的問候。

回答

47

可以在UIImagePickerControllerDelegate委託執行

- (void) imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    //obtaining saving path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"]; 

    //extracting image from the picker and saving it 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    if ([mediaType isEqualToString:@"public.image"]){ 
     UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
     NSData *webData = UIImagePNGRepresentation(editedImage); 
     [webData writeToFile:imagePath atomically:YES]; 
    } 
} 

這是使用下面的代碼全部

+3

我不得不使用[infoObjectForKey:UIImagePickerControllerOriginalImage];而不是UIImagePickerControllerEditedImage – 2013-11-26 20:22:32

+0

準確地說,它取決於你允許編輯與否,picker.allowsEditing = YES; – khunshan 2014-03-18 08:25:16

8

根據您要保存的文件格式,你可以使用

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES]; 

OR

[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES]; 
3

這是將UIImage保存到文檔目錄中的代碼。您可以使用此代碼didFinishPickingImage委託方法:

// Create paths to output images 
NSString *pngPath = [NSHomeDirectory(); 
stringByAppendingPathComponent:@"Documents/Test.png"]; 
NSString *jpgPath = [NSHomeDirectory(); 
stringByAppendingPathComponent:@"Documents/Test.jpg"]; 

// Write a UIImage to JPEG with minimum compression (best quality) 
// The value 'image' must be a UIImage object 
// The value '1.0' represents image compression quality as value from 0.0 to 1.0 
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; 

// Write image to PNG 
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; 

// Let's check to see if files were successfully written... 

// Create file manager 
NSError *error; 
NSFileManager *fileMgr = [NSFileManager defaultManager]; 

// Point to Document directory 
NSString *documentsDirectory = [NSHomeDirectory(); 
stringByAppendingPathComponent:@"Documents"]; 

// Write out the contents of home directory to console 
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

編輯

您還可以使用:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

找到路徑到您的申請文件目錄,而不是NSHomeDierctory。

1

更新knuku對雨燕3.0

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    //obtaining saving path 
    let fileManager = FileManager.default 
    let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first 
    let imagePath = documentsPath?.appendingPathComponent("image.jpg") 

    // extract image from the picker and save it 
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     try! UIImageJPEGRepresentation(pickedImage, 0.0)?.write(to: imagePath!) 
    } 
    self.dismiss(animated: true, completion: nil)   
} 

答案在這裏將圖像保存爲JPEG格式,但你也可以將其保存爲PNG。 0.0參數代表壓縮,如果你想要最好的使用1.0,這是最低的質量。

相關問題