Objective c 替换相册照片中的图像

Objective c 替换相册照片中的图像,objective-c,ios,image,uiimage,Objective C,Ios,Image,Uiimage,每个人, 实际上,我正在从我的相册中拍照。我在上面画画,完成后,我把它保存到我的相册里。它工作得非常好。问题是,当我保存这个新图像时,设备会将其保存到另一个图像中。我想知道的是,是否有可能修改现有的图像?要替换它来创建一个新的吗?或者,如果不可能,我可以删除库中的图像并创建一个新图像吗 最好的方法是使用URL // I take the image ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; [assetLibr

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

最好的方法是使用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");}];
//然后我换了一个新的


提前谢谢你

非常感谢Combinatory,没有你我永远找不到它。 因此,只有当应用程序创建资产时,代码才起作用。方法如下:

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

它似乎删除了以前的图像,并用新名称创建了一个新的图像,这不是优化,但它可以工作…

多亏了Combinational,没有你我永远找不到它。 因此,只有当应用程序创建资产时,代码才起作用。方法如下:

- (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");
}];
}
似乎删除了以前的图像,并使用新名称创建了一个新图像,这不是优化,但它可以工作…

使用PHPhotoLibrary,您可以从相机卷中替换特定相位集的图像

你别忘了在Info.plist文件中添加这个代码

使用PHPhotoLibrary,您可以替换摄影机卷中特定相位集的图像

你别忘了在Info.plist文件中添加这个代码


您是否尝试过设置setImageData:metadata:completionBlock:?有两种方法很有趣:-setImageData-writeModifiedImageDataToSavedPhotosAlbum,但我认为它们只是重新创建了一个新图像。它们不会取代以前的设置。setImageData的文档说明,如果图像可编辑,它会写入图像。您可以先使用可编辑属性检查图像是否可编辑,然后使用初始问题中使用的assetForURL调用。在resultBlock中,您有要写入的资产。etry asset.defaultRepresentation.metadata您是否尝试过Alaset setImageData:metadata:completionBlock:?有两种方法很有趣:-setImageData-writeModifiedImageDataToSavedPhotosAlbum,但我认为它们只是重新创建了一个新图像。它们不会取代以前的设置。setImageData的文档说明,如果图像可编辑,它会写入图像。您可以先使用可编辑属性检查图像是否可编辑,然后使用初始问题中使用的assetForURL调用。在resultBlock中,您有要写入的资产。asset.defaultRepresentation.metadata
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your photo library to let you select a picture.</string>