How to move an image from asset library to an application specific private location?

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 the error as Error Domain=NSCocoaErrorDomain Code=262 "The operation couldn’t be completed. (Cocoa error 262.)" UserInfo=0xb49a680 {NSURL=assets-library://asset/asset.JPG?id=6FAC823A-33CB-489B-A125-714FBA5F0EE8&ext=JPG}

Any ideas?

Answers


Docs (and Google). This error code corresponds to the NSFileReadUnsupportedSchemeError constant - i. e. you can't just use an assets-library:// URL to move a file using NSFileManager. (The same is the case regarding ipod-library:// URLs.) You have to use the AssetsLibrary framework to get the data of the file and then write it to a file using - [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];
    }];

Need Your Help

Help with getting data from resized image

c# asp.net image-resizing

I'm using an ASP.NET file upload control and then resizing an image and storing the new image in a new Bitmap. Here is the code I have so far:

If a column has string to execute syntax

php sql if-statement case

I wanted to know if there was a way to do an IF ELSE statement within SQL syntax or if someone could help me with CASE. Basically if a file name (string) is found in the file_url column then change...