How to transfer an image from a resource library to a private location of a specific application?

Here is the code snippet I tried:

[library    writeImageToSavedPhotosAlbum:self.checkInImage.CGImage metadata:self.metadata
            completionBlock             :^(NSURL * assetURL, NSError * error) {
        NSLog (@"Asset url = %@", assetURL);
         NSError *err;
        NSString *docDir = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *jpgFilePath = [NSString stringWithFormat:@"%@/test.jpg", docDir];
        NSFileManager *fileManager = [NSFileManager defaultManager];
                [fileManager moveItemAtURL:assetURL toURL:[NSURL URLWithString:jpgFilePath] error:&err];
                NSLog(@"error is %@", err);
    }];

I get an error like Error domain = NSCocoaErrorDomain Code = 262 "The operation could not be completed. (Cocoa error 262.)" UserInfo = 0xb49a680 {NSURL = assets-library: //asset/asset.JPG? Id = 6FAC823A-33CB-489B-A125 -714FBA5F0EE8 & int = JPG}

Any ideas?

+5
source share
2 answers

This is how I solved the problem:

1) I saved the file without metadata in some place and saved its urlpath.

2) Then the following code fragment was used to save the same metadata file. Here fileURL is the urlpath obtained from step 1.

CFURLRef url = CFURLCreateFilePathURL(NULL, (CFURLRef)fileURL, NULL);
// Creating Image source from image url
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(url, NULL);

// This will be the data CGImageDestinationRef will write into
CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData(data, CFSTR("public.jpeg"), 1, NULL);

BOOL success = (imageSource != NULL);

require(success, cleanup);

success = (imageDestination != NULL);
require(success, cleanup);

// Add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
NSDictionary *metadata = someObj.metadata;
CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, (CFDictionaryRef)metadata);

// Tell the destination to write the image data and metadata into our data object.
success = CGImageDestinationFinalize(imageDestination);

require(success, cleanup);

// Write the data into the url
[(NSMutableData *) data writeToURL:fileURL atomically:YES];

Cleaning:

if (data) {
    CFRelease(data);
    data = NULL;
}

if (imageSource) {
    CFRelease(imageSource);
    imageSource = NULL;
}

if (imageDestination) {
    CFRelease(imageDestination);
    imageDestination = NULL;
}

return success;

, . H2C03 , . , , . .

+1

( Google). NSFileReadUnsupportedSchemeError - i. . URL assets-library:// NSFileManager. ( URL- ipod-library://.) AssetsLibrary , - [NSData writeToFile:atomically:].

ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib assetForUrl:theURL // the asset URL you have obtained, NSURL object
    resultBlock:^(ALAsset *asset) {
        // get data
        ALAssetRepresentation *repr = [asset defaultRepresentation];
        CGImageRef cgImg = [repr fullResolutionImage];
        NSString *fname = [repr fileName];
        UIImage *img = [UIImage imageWithCGImage:cgImg];
        NSData *data = UIImagePNGRepresentation(img);
        [data writeToFile:[@"BaseDirectory/" stringByAppendingPathComponent:fname]
            atomically:YES];
        [lib release];
    }
    failureBlock:^(NSError *error) {
        // recover from error, then
        [lib release];
    }];
+5

All Articles