SetImageData not working in iOS 8.3

I am using setImageData to delete a photo like this:

[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error)
{
    // Do something
}];

      

This code worked fine in iOS 8.2 and earlier.

But in 8.3 it gives an error:

@ "Domain error = ALAssetsLibraryErrorDomain Code = -3311 \" User denied access \ "UserInfo = 0x175061ac0 {NSLocalizedFailureReason = User denied application access to their media., NSLocalizedDescription = User denied access, NSUnderlyingError = 0x17025d700 \" operation could not be completed. (ALAssetsLibraryErrorDomain error -3311.) \ "}"

I tried to replace the image and metadata data fields with some valid image data instead of "nil". However, it gives the same error!

Is this some kind of bug in iOS 8.3? Is there a workaround?

Thanks in anticipation.

Other important information:

[PHPautoLibrary authorizationStatus] returns "PHAuthorizationStatusAuthorized". [ALAssetsLibrary authorizationStatus] also returns "ALAuthorizationStatusAuthorized".

+2


source to share


3 answers


As far as I know, the setImageData method was never intended to be used as a method to remove assets. It's possible that on iOS 8.3 Apple patched things up, so this no longer works.

I recommend that you study the "Photos" structure, which includes a dedicated method for removing assets. Here's an example:



-(void)deleteAssetWithURL:(NSString*)assetURLString
{
    NSURL *assetURL = [NSURL URLWithString:assetURLString];
    if (assetURL == nil)
    {
        return;
    }

    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
    if (result.count > 0)
    {
        PHAsset *phAsset = result.firstObject;
        if ((phAsset != nil) && ([phAsset canPerformEditOperation:PHAssetEditOperationDelete]))
        {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
            {
                [PHAssetChangeRequest deleteAssets:@[phAsset]];
            }
                                              completionHandler:^(BOOL success, NSError *error)
             {
                 if ((!success) && (error != nil))
                 {
                     NSLog(@"Error deleting asset: %@", [error description]);
                 }
             }];
        }
    }
}

      

When using the Photos framework, be sure to link Photos.framework

in your target and also import the title in the source file:#import <Photos/Photos.h>

+2


source


I have a similar problem and I was unable to solve it. I think this is a bug in 8.3 with AssetsLibrary and will prompt you to submit a bug report to Apple like me:



Can't edit photo metadata with AssetsLibrary in iOS 8.3 (works in 8.2)

0


source


The user denied the application access to their media

This explains why you have the error. As far as the system is concerned, you don't have access to your photo library.

You need to look at the authorization status check and ask if necessary how to show in Apple Doc

You need something like:

if([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatus.PHAuthorizationStatusAuthorized){  
   [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
   {
      if (status != PHAuthorizationStatus.PHAuthorizationStatusAuthorized)
      {
        //Fail
      }
      else
      {
        //SetImageData
      }
   }
}

      

-1


source







All Articles