如何将图像从资产库移动到特定于应用程序的私有位置?

时间:2012-09-09 06:46:29

标签: ios alassetslibrary

以下是我尝试的代码段:

[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);
    }];

我得到的错误是 错误域= NSCocoaErrorDomain代码= 262“操作无法完成。(Cocoa错误262.)”UserInfo = 0xb49a680 {NSURL = assets-library://asset/asset.JPG?id = 6FAC823A-33CB-489B-A125 -714FBA5F0EE8&安培; EXT = JPG}

有什么想法吗?

2 个答案:

答案 0 :(得分:5)

Docs (and Google).此错误代码对应于NSFileReadUnsupportedSchemeError常量 - i。即您不能只使用assets-library:// URL来使用NSFileManager移动文件。 (关于ipod-library:// URL的情况也是如此。)您必须使用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];
    }];

答案 1 :(得分:1)

这就是我解决问题的方法:

1)我在某个位置保存了没有元数据的文件,并存储了其urlpath。

2)然后使用以下代码来保存带有元数据的同一文件。这里fileURL是从步骤1获得的urlpath。

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];

清理:

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

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

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

return success;

希望这会有所帮助。但我想提出H2C03的非常有用的建议,这有助于达成这个解决方案。请帮我怎么做。感谢。