addResourceWithType not working for RAW + JPEG (restoring a RAW+JPEG asset not possible)

Hi,


this question refers to classes PHAssetCreationRequest & PHAssetResource, which are new in iOS 9.

From the documentation:

"This class (PHAssetCreationRequest) works in terms of the raw data resources that together form an asset, so you can use it together with the PHAssetResource class to perform a complete copy (or backup and restore) of an asset’s underlying resources."


I tried the Backup & Restore procedure for a RAW+JPEG PHAsset, which has been imported from a camera using the Camera Connection Kit.


1) Backing up works fine.

For a JPEG+RAW asset the method assetResourcesForAsset: of PHAssetResource returns

<PHAssetResource: 0x126d83f60> type=photo uti=public.jpeg filename=P1080727.JPG assetLocalIdentifier=0C56E8A5-ECAB-4474-91E4-2688885A6CF6/L0/001,
<PHAssetResource: 0x128803fa0> type=photo_alt uti=com.panasonic.rw2-raw-image filename=P1080727.RW2 assetLocalIdentifier=0C56E8A5-ECAB-4474-91E4-2688885A6CF6/L0/001)

So the resource type for the JPEG is PHAssetResourceTypePhoto, while the resource type for the RAW is PHAssetResourceTypeAlternatePhoto


2) Trying to restore the RAW+JPEG asset using addResourceWithType fails.

The follwing code is used to perform the restore:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

  PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
  [request addResourceWithType:PHAssetResourceTypePhoto fileURL:[[NSBundle mainBundle] URLForResource:@"P1080727" withExtension:@"JPG"] options:nil];
  [request addResourceWithType:PHAssetResourceTypeAlternatePhoto fileURL:[[NSBundle mainBundle] URLForResource:@"P1080727" withExtension:@"RW2"] options:nil];

   } completionHandler:^(BOOL success, NSError * _Nullable error) {
       NSLog(@"PHPhotoLibrary performChanges result: Success: %i, Error: %@",success,error);
}];


The performChanges-Block completes with a Cocoa Error -1 without any further information.

So it does not seem to be possible (in contrast to the documentation) to restore a PHAsset with both a JPEG and RAW resource file.

Additonal note:

[PHAssetCreationRequest supportsAssetResourceTypes:@[@(PHAssetResourceTypePhoto),@(PHAssetResourceTypeAlternatePhoto)]];

also returns false. Also using other AssetResourceTypes I couldn't find any combinations, for which this function returns true.

I reported the issue also as radar 22701890. The behaviour is the same on iOS 9 GM and iOS 9 Beta 1.


Am I missinterpreting something on how these classes should be used ?



Cheers,


Hendrik

Hi,


Have you find a solution for this issue? I'm facing the same issue.


Cheers

Unforuntely not. The radar I filled back then is still "Open" and the issue still present in iOS 9.4 Beta 4. Maybe you can fill a radar too - that usually increases the attention:)


Cheers,


Hendrik

if you want to save Raw photo to iPhone/iPad system album, firstly,you can download Raw file to Documents directory,and then you will get its path.


[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

// fullPath is Raw file path

[PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:[NSURL fileURLWithPath:fullPath]];

}completionHandler:^( BOOL success, NSError *error ) {

if(success){

// update UI on main thread

}else{

NSLog(@"error: %@",error.description);

}

// you can delete Raw file in Documents to free the space.

}];

@dogvscat: Importing a RAW file is not the issue. It's about restoring an RAW+JPEG, which appears as one PHAsset with two resources in the Photo Library.

Cheers,


Hendrik

Okay, it looks like it is at least partially fixed now (in iOS 11).


The asset creation succeeds if the Photo resource (JPEG) is added from Data and the AlternatePhoto resource (DNG) is added from a URL. It does *not* work if the AlternatePhoto resource is added from Data. (It might work if both resources are added from URLs, but I did not test that case).


    __block NSString* localIdentifierOfNewAsset;
    NSString* filename = [PHPhotoLibrary sharedPhotoLibrary].nextAssetFilename;
   
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCreationRequest* creationRequest = [PHAssetCreationRequest creationRequestForAsset];
       
        if (processedImageData) {
            PHAssetResourceCreationOptions* photoResourceOptions = [PHAssetResourceCreationOptions new];
            photoResourceOptions.originalFilename = [filename stringByAppendingPathExtension:@"JPG"];
            photoResourceOptions.uniformTypeIdentifier = (NSString*)kUTTypeJPEG;
            [creationRequest addResourceWithType:PHAssetResourceTypePhoto data:processedImageData options:photoResourceOptions];
        }
       
        if (dngData) {
#if TRUE
            // This works
            NSURL *temporaryDNGFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[filename stringByAppendingPathExtension:@"DNG"]]];
            if ([dngData writeToURL:temporaryDNGFileURL atomically:YES]) {
               
                PHAssetResourceCreationOptions* dngResouceOptions = [PHAssetResourceCreationOptions new];
                dngResouceOptions.shouldMoveFile = true;
                [creationRequest addResourceWithType:PHAssetResourceTypeAlternatePhoto fileURL:temporaryDNGFileURL options:dngResouceOptions];
            }
#else
            // This does not work
            PHAssetResourceCreationOptions* dngResouceOptions = [PHAssetResourceCreationOptions new];
            dngResouceOptions.uniformTypeIdentifier = @"com.adobe.raw-image";
            [creationRequest addResourceWithType:PHAssetResourceTypeAlternatePhoto data:dngData options:dngResouceOptions];
#endif
           
        }
        localIdentifierOfNewAsset = creationRequest.placeholderForCreatedAsset.localIdentifier;
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        PHAsset* asset;
        if (success) {
            asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifierOfNewAsset] options:nil].firstObject;
        }
        if (asset) {
        }
    }];
addResourceWithType not working for RAW &#43; JPEG (restoring a RAW&#43;JPEG asset not possible)
 
 
Q