mob*_*jug 5 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);
}];
Run Code Online (Sandbox Code Playgroud)
我收到错误,因为错误域= NSCocoaErrorDomain Code = 262"操作无法完成.(Cocoa错误262.)"UserInfo = 0xb49a680 {NSURL = assets-library://asset/asset.JPG?id = 6FAC823A- 33CB-489B-A125-714FBA5F0EE8&EXT = JPG}
有任何想法吗?
小智 5
文档(和谷歌).此错误代码对应于NSFileReadUnsupportedSchemeError常量 - 即您不能仅使用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];
}];
Run Code Online (Sandbox Code Playgroud)
这就是我解决问题的方法:
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];
Run Code Online (Sandbox Code Playgroud)
清理:
if (data) {
CFRelease(data);
data = NULL;
}
if (imageSource) {
CFRelease(imageSource);
imageSource = NULL;
}
if (imageDestination) {
CFRelease(imageDestination);
imageDestination = NULL;
}
return success;
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。但我想为 H2C03 的非常有用的建议投票,这有助于达成这个解决方案。请帮助我该怎么做。谢谢。
| 归档时间: |
|
| 查看次数: |
6601 次 |
| 最近记录: |