5

这是我尝试的代码片段:

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

我收到错误,因为 Error Domain=NSCocoaErrorDomain Code=262 “操作无法完成。(Cocoa 错误 262。)” UserInfo=0xb49a680 {NSURL=assets-library://asset/asset.JPG?id=6FAC823A- 33CB-489B-A125-714FBA5F0EE8&ext=JPG}

有任何想法吗?

4

2 回答 2

5

文档(和谷歌)。此错误代码对应于NSFileReadUnsupportedSchemeError常量 - 即您不能只使用assets-library://URL 来使用 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];
    }];
于 2012-09-09T06:51:26.723 回答
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 提出非常有用的建议,这有助于达成这个解决方案。请帮我怎么做。谢谢。

于 2012-09-26T19:04:32.040 回答