简体   繁体   English

无法使用ALAssets返回的路径URL加载图像

[英]Cannot load image with the path URL returned by ALAssets

I am writing an image in iPad using ALAssets. 我正在使用ALAssets在iPad中写入图像。 When it finish I try to create an UIImage with the returned URL but it won't load. 完成后,我尝试使用返回的URL创建UIImage,但不会加载。 This is the code: 这是代码:

LAssetsLibrary *library = [[ALAssetsLibrary alloc] init];  
    [library writeImageToSavedPhotosAlbum:[anImage CGImage] orientation:(ALAssetOrientation)[anImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){  
        if (!error) {
            CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef) [NSURL fileURLWithPath:[assetURL absoluteString]], NULL);

My purpose is to save an image to the device, then convert it to another format using ImageIO and finally send it to a web service. 我的目的是将图像保存到设备,然后使用ImageIO将其转换为另一种格式,最后将其发送到Web服务。 CGImageSourceRef is null, I also tried with standard UIImage with the same result. CGImageSourceRef为null,我也尝试了使用相同结果的标准UIImage。

What I am doing wrong here? 我在这里做错了什么?

EDIT: The problem is when creating the CFURLRef. 编辑:问题是创建CFURLRef时。 If I do 如果我做

CGImageSourceCreateWithURL((CFURLRef) assetURL, NULL);

I got this error 我得到这个错误

ImageIO: CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource failed with error code -11. ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-11。

But if I try to convert the URL with 但是,如果我尝试使用

[NSURL fileURLWithPath:[assetURL absoluteString]]

the path is changed to 路径更改为

assets-library:/asset/asset.JPG%3Fid=57BBBA99-E7BF-4DB7-839E-F915005E6DFA&ext=JPG -- file://localhost/

I cannot find how to properly create the CFURLRef needed by the method. 我找不到如何正确创建该方法所需的CFURLRef的方法。 I tried printing all the conversions I could think of and this are the results 我尝试打印所有我能想到的转换,这就是结果

[assetURL relativePath]
[assetURL relativeString]
[assetURL absoluteURL]
[assetURL absoluteString]
 /asset.JPG ,
 assets-library://asset/asset.JPG?id=57BBBA99-E7BF-4DB7-839E-F915005E6DFA&ext=JPG
 assets-library://asset/asset.JPG?id=57BBBA99-E7BF-4DB7-839E-F915005E6DFA&ext=JPG
 assets-library://asset/asset.JPG?id=57BBBA99-E7BF-4DB7-839E-F915005E6DFA&ext=JPG

[NSURL fileURLWithPath:[assetURL relativePath]]
[NSURL fileURLWithPath:[assetURL relativeString]]
[NSURL fileURLWithPath:[assetURL absoluteString]]
file://localhost/asset.JPG
assets-library:/asset/asset.JPG%3Fid=57BBBA99-E7BF-4DB7-839E-F915005E6DFA&ext=JPG -- file://localhost/
assets-library:/asset/asset.JPG%3Fid=57BBBA99-E7BF-4DB7-839E-F915005E6DFA&ext=JPG -- file://localhost/

Help please, I am stuck with this :-( 请帮助,我被困在这个:-(

This is what I did for my case. 这就是我为案件所做的。

    UIImage* anImage;   //this is the original image    
    NSData * imgData = UIImageJPEGRepresentation(anImage, 0.7);
    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef) imgData, NULL);

    NSMutableData *data = [NSMutableData data];
    CFStringRef imageType = CFSTR("com.microsoft.bmp");
    CGImageDestinationRef myImageDest = CGImageDestinationCreateWithData((CFMutableDataRef) data, imageType, 1, nil);

    //Convert!
    CGImageDestinationAddImageFromSource(myImageDest, src, 0, myOptions);
    CGImageDestinationFinalize(myImageDest);
    //Freeing things
    CFRelease(myImageDest);
    CFRelease(src);

But this just converts the image, it doesn't store it in any file... Not sure this should be an answer to the original question. 但这只是转换图像,不会将其存储在任何文件中……不确定这是否是原始问题的答案。

If you already have an ALAsset and your goal is a CGImageRef you can do something like this. 如果您已经有了ALAsset并且目标是CGImageRef,则可以执行以下操作。

ALAssetRepresentation* rep = [asset defaultRepresentation];

NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
                         (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
                         (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways, 
                         (id)[NSNumber numberWithDouble:400], (id)kCGImageSourceThumbnailMaxPixelSize, 
                         nil];

CGImageRef image = [rep CGImageWithOptions:options];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM