2012-10-18 75 views
1

I每个身体, 实际上,我正在从我的相册拍照。我正在画它,然后当我完成时,我将它保存到我的相册中。它工作得很好。问题是,当然,当我保存这个新的图像时,该设备将其保存到另一个图像中。我想知道的是,是否可以修改现有的图像?像取代它,而不是创建一个新的?或者如果不可能,我可以删除库中的图像并创建一个新的图像?更换相册照片中的图像

完美的方式是使用URL。

// I take the image 
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; 
[assetLibrary assetForURL:asseturl 
     resultBlock:^(ALAsset *asset){NSLog(@"I got my image");} 
     failureBlock:^(NSError *error){NSLog(@"Error");}]; 

//然后我用一个新的 更换???

谢谢你提前

+0

你试过ALAsset setImageData添加以下代码:元数据:completionBlock:? – combinatorial

+0

有趣的两种方法: - setImageData - writeModifiedImageDataToSavedPhotosAlbum 但我认为他们只是重新创建一个新的图像。它们不替换前一个 – pierre23

+0

setImageData的文档说它编写图像如果它是可编辑的,您可以先检查它是否可以使用editable属性进行编辑 – combinatorial

回答

4

非常感谢组合,我永远不会发现没有你。 因此,只有当应用程序创建资产时,代码才有效。这是方法:

- (void)replaceImageWithUrl:(NSString *)url withImage:(UIImage*)image 
{ 
    ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; 
    NSURL *asseturl = [NSURL URLWithString:url]; 
    [assetLibrary assetForURL:asseturl 
       resultBlock:^(ALAsset *asset) 
{ 

      ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation]; 
      NSLog(@"Image name : %@",assetRepresentation.filename); 

      if([asset isEditable]) 
      { 
       NSLog(@"Asset editable"); 

       [asset setImageData:UIImageJPEGRepresentation(image, 1.0) metadata:asset.defaultRepresentation.metadata completionBlock:^(NSURL *assetURL, NSError *error) 
       { 
        if (error) 
         NSLog(@"error %@",error); 
        else 
         NSLog(@"assetURL %@", assetURL); 
      }]; 
      } 
      else 
      { 
       NSLog(@"Not editable"); 
      } 
     } 
     failureBlock:^(NSError *error){NSLog(@"FAILED"); 
}]; 
} 

看来删除以前的形象,创造一个新的一个新的名字,这不是最佳,但它的工作原理...

0

使用PHPhotoLibrary可以更换图像特别相机胶卷上的PHasset。

-(void) replaceimagefromcameraroll :(PHAsset*)replaceasset { 
    PHAsset *asset = replaceasset; 
    NSString *localStr=asset.localIdentifier; 
    NSRange range = [localStr rangeOfString:@"/"]; 
    NSString *newString = [localStr substringToIndex:range.location]; 
    NSString *appendedString=[NSString stringWithFormat:@"%@%@%@",@"assets-library://asset/asset.JPG?id=",newString,@"&ext=JPG"]; 
    NSLog(@"%@ phasset ",appendedString); 

    NSURL *ReplaceAsseturl = [NSURL URLWithString:appendedString]; 

    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[ReplaceAsseturl] options:nil]; 
    PHAsset *assetReplace = result.firstObject; 
    if ([assetReplace canPerformEditOperation:PHAssetEditOperationContent]) 
    { 
     [asset requestContentEditingInputWithOptions:nil completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) { 

      PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput]; 

      UIGraphicsBeginImageContextWithOptions(_imgPreviewView.bounds.size, NO, 0.0); 

      [_imgPreviewView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
      UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 

      UIGraphicsEndImageContext(); 

      NSData *outputData = UIImageJPEGRepresentation(img, img.scale); 

      PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"AdjustementDataIdentifier" formatVersion:@"1.0" data:outputData]; 
      contentEditingOutput.adjustmentData = adjustmentData; 
      BOOL wrote = [outputData writeToURL:contentEditingOutput.renderedContentURL options:NSDataWritingAtomic error:nil]; 
      [SVProgressHUD dismiss]; 
      if (wrote) 
      { 
       [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
        PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset]; 
        request.contentEditingOutput = contentEditingOutput; 

       } completionHandler:^(BOOL success, NSError *error) { 
        // console output : 1 
        if (success){ 
         NSLog(@"success : %@", @(success)); 
         [SVProgressHUD showSuccessWithStatus:ckSuccesReplaceImage]; 
        } 
        else{ 
         NSLog(@"error : %@", error); 
        } 
       }]; 
      } 
     }]; 
    } 
} 

雅不要忘了在Info.plist文件

<key>NSPhotoLibraryUsageDescription</key> 
<string>$(PRODUCT_NAME) would like to access your photo library to let you select a picture.</string> 
相关问题