替换相册照片中的图像

时间:2012-10-18 16:38:24

标签: objective-c ios image uiimage

每个人, 实际上,我正在从我的专辑中拍照。我正在画它然后当我完成时,我将它保存到我的专辑中。它工作得很好。问题是,当我保存这个新图像时,设备将其保存到另一个图像中。我想知道的是,是否可以修改现有图像?比如替换它来创建一个新的?或者,如果不可能,我可以删除库中的图像并创建一个新图像吗?

完美的方法是使用网址。

// 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");}];

//然后我用新的替换它    ???

提前谢谢

2 个答案:

答案 0 :(得分: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");
}];
}

似乎删除了上一张图片并创建了一个带有新名称的新图片,它不是优化但是有效...

答案 1 :(得分: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>