如何将图像从资产库移动到特定于应用程序的私有位置? [英] How to move an image from asset library to an application specific private location?

查看:77
本文介绍了如何将图像从资产库移动到特定于应用程序的私有位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我尝试的代码段:

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

我收到错误
错误域= NSCocoaErrorDomain代码= 262操作无法'完成。(可可错误262.)UserInfo = 0xb49a680 {NSURL = assets-library://asset/asset.JPG?id = 6FAC823A-33CB-489B-A125-714FBA5F0EE8& ext = JPG}

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}

任何想法?

推荐答案

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

This is how I solved the issue:

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

1) I saved the file without metadata in some location, and stored its urlpath.

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

2) Then used the following piece of code to save the same file with metadata. Here fileURL is the urlpath obtained from step 1.

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的非常有用的建议,这有助于达成这个解决方案。请帮我怎么做。谢谢。

Hope this helps. But I would like to upvote H2C03 for very helpful suggestions, which helped to arrive at this solution. Please help me how to do it. Thanks.

这篇关于如何将图像从资产库移动到特定于应用程序的私有位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆