0

我可以輕鬆保存到相機膠捲使用相機拍攝的圖像。 但我不想保存圖像,如果這是從相機膠捲選擇,因爲它會創建一個副本。如何避免將從相機膠捲拾取的圖像保存到相機膠捲? [iOS]

如何做到以下幾點,並選擇性地保存到相機膠捲?

編輯1: 正如rmaddy說我可以: 如果picker.sourceType等於UIImagePickerControllerSourceTypeCamera,則僅保存到相機膠捲。

但如果我這樣做,我將無法獲得所選圖像的信息,主要是文件名。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    [self.myImageView setImage: image]; 
    [self.myImageView setContentMode:UIViewContentModeScaleAspectFit]; 
    ALAssetsLibrary *al=[[ALAssetsLibrary alloc] init]; 

    // ------> Saving to Camera Roll <------------ 

    [al writeImageToSavedPhotosAlbum:[image CGImage] metadata:nil completionBlock:^(NSURL *assetURL,NSError *error) { 

      ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myAsset) { 
       ALAssetRepresentation *rep = [myAsset defaultRepresentation]; 
       NSString *fileName = [rep filename]; 
       ImageInfo *imgInfo = [[ImageInfo alloc] initWithEventId:0 name:fileName image:UIImageJPEGRepresentation(image, 1.0)]; 
       imgInfo.isDirty = YES; 
       [self.imagesArray addObject:imgInfo]; 
       [self dismissViewControllerAnimated:YES completion:nil]; 
       NSLog(@"Image from Camera %@ added to imageArray",fileName); 
      }; 

      ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init]; 
      [assetsLibrary assetForURL:assetURL 
          resultBlock:resultblock 
          failureBlock:nil]; 

    }]; 
} 
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) 
{ 
    // Code here to support video if enabled 
} 

編輯2:

解決了這個mod: (我知道有一個重複的代碼一點點,我稍後會嘗試改善它)

if (newMedia){ 
     ALAssetsLibrary *al=[[ALAssetsLibrary alloc] init]; 
     [al writeImageToSavedPhotosAlbum:[image CGImage] metadata:nil completionBlock:^(NSURL *assetURL,NSError *error) 
     {     
      ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myAsset) 
      { 
       ALAssetRepresentation *rep = [myAsset defaultRepresentation]; 
       //NSString *fileName = [rep filename]; 
       NSString *imgUrl = [[rep url] absoluteString]; 
       ImageInfo *imgInfo = [[ImageInfo alloc] initWithEventId:0 imgUrl:imgUrl image:UIImageJPEGRepresentation(image, 1.0)]; 
       imgInfo.isDirty = YES; 
       [self.imagesArray addObject:imgInfo]; 
       [self dismissViewControllerAnimated:YES completion:nil]; 
       NSLog(@"Image from Camera %@ added to imageArray",imgUrl); 
      }; 

      ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init]; 
      [assetsLibrary assetForURL:assetURL 
          resultBlock:resultblock 
          failureBlock:nil]; 
     }]; 
} else { 
     NSString* imgUrl = UIImagePickerControllerReferenceURL; 
     ImageInfo *imgInfo = [[ImageInfo alloc] initWithEventId:0 imgUrl:imgUrl image:UIImageJPEGRepresentation(image, 1.0)]; 
     imgInfo.isDirty = YES; 
     [self.imagesArray addObject:imgInfo]; 
     NSLog(@"Image from Camera Roll %@ added to imageArray",imgUrl); 
     [self dismissViewControllerAnimated:YES completion:nil]; 
} 

回答

3

不僅節省到相機膠捲如果picker.sourceType等於UIImagePickerControllerSourceTypeCamera

+0

對,但如果我這樣做,我將無法獲得所選圖像的信息,主要是文件名。 – Alvin

+0

@AlvinPastore使用'info'字典的'UIImagePickerControllerReferenceURL'鍵來獲取所選圖像的原始URL。從照片庫中獲取圖像時,使用它代替'assetURL'。 – rmaddy

+0

謝謝。我決定使用URL而不是文件名,並修改代碼以直接處理信息(如您所建議的),而不是使用ALAssetLibrary代碼... – Alvin

相關問題