2014-01-15 63 views
1

我想保存2個圖像,因爲他們被挑選爲單獨的名稱。UIImagePickerController,以不同的名稱保存2個圖像到文檔目錄

我用這個指南,把我的UIImagePickerController:guide

基本上它是一個管理2個UIImageViews的,是有可能把它存2張不同的圖片,不同的名字一個的UIImagePickerController?或者我應該嘗試使uiimagepickercontroller不同?

這是我的代碼,它不適用於保存。

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

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]; 
} 
switch (_selectedPhotoType) { 
    case imageView1Type: 
     imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     break; 
    case imageView2Type: 
     imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     break; 
    default: 
     break; 
} 

[self dismissViewControllerAnimated:YES completion:^{ }]; 
} 
+0

,所以你想在同一攝取的圖像保存到2個不同的文件嗎? – heximal

+0

簡單的問題你想要一個按鈕有兩個選擇器或兩個按鈕與一個選擇器..... – Spynet

+0

@heximal不,我有2個uiimageviews,當我點擊每個按鈕下,我選擇2個不同的圖像...我想將它們保存在每個根據名稱 – KennyVB

回答

0

看看這個實現。你需要嗎?

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

     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]; 
     UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
     NSData *webData = UIImagePNGRepresentation(editedImage); 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     switch (_selectedPhotoType) { 
      case imageView1Type: { 
       NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo1.png"]; 
       imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     } 
       break; 
      case imageView2Type: { 
       NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo2.png"]; 
       imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     } 
       break; 
      default: 
       break; 
     } 
     [webData writeToFile:imagePath atomically:YES]; 

     [self dismissViewControllerAnimated:YES completion:^{ }]; 
     } 
+0

不起作用的原因? – KennyVB

+0

webData是否爲零? – KennyVB

0

想通了很少的幫助@Heximal

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

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]; 
switch (_selectedPhotoType) { 
    case imageView1Type: { 
     NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo1.png"]; 
     imageView1.image = [info objectForKey:UIImagePickerControllerEditedImage]; 
     if ([mediaType isEqualToString:@"public.image"]){ 
      UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
      NSData *webData = UIImagePNGRepresentation(editedImage); 
      [webData writeToFile:imagePath atomically:YES]; 
     } 

    } 
     break; 
    case imageView2Type: { 
     NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo2.png"]; 
     imageView2.image = [info objectForKey:UIImagePickerControllerEditedImage]; 
     if ([mediaType isEqualToString:@"public.image"]){ 
      UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
      NSData *webData = UIImagePNGRepresentation(editedImage); 
      [webData writeToFile:imagePath atomically:YES]; 
     } 
    } 
     break; 
    default: 
     break; 
} 

[self dismissViewControllerAnimated:YES completion:^{ }]; 
} 
相關問題