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

could not find member subscript

ios swift xcode6

I am trying to execute the following code and getting the error Could not find member 'subscript on Xcode6

Enable CORS in Azure Mobile Services - OPTIONS not authorized

c# azure asp.net-web-api cors azure-mobile-services

I am creating .Net web services using Azure Mobile Services. The services themselves work fine, but I want to enable CORS.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.