2014-01-31 110 views
0

我需要读取图像的属性而不加载或下载它。实际上,我已经实现了一个使用CGImageSourceCreateWithUrl来实现这个功能的简单方法。 我的问题是它总是返回错误,因为看起来imageSource是空的。那么我能做些什么来解决它? 在NSURL对象中,我传递的URL如下: “http://www.example.com/wp-content/uploads/image.jpg”,还包含ALAssets库Id,用于检索手机内部的图像,如“assets-library://asset/asset.JPG?id = E5F41458-962D-47DD-B5EF- E606E2A8AC7A & ext = JPG“。 这是我的方法:CGImageSourceCreateWithURL总是返回NULL

-(NSString *) getPhotoInfo:(NSString *)paths{ 
    NSString *xmlList = @“test”; 

    NSURL * imageFileURL = [NSURL fileURLWithPath:paths]; 
    NSLog(@"imageFileURL %@", imageFileURL); 
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL); 
    if (imageSource == NULL) { 
    // Error loading image 
    NSLog(@"Error loading image"); 
    } 
    CGFloat width = 0.0f, height = 0.0f; 
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL); 
    NSLog(@"image source %@", imageSource); 

    return xmlList; 
} 

我已经看到了这个职位,尝试修复它,但似乎没有工作:

在我的项目ARC已启用。

感谢

回答

5

如果您传递字符串“http://www.example.com/wp-content/uploads/image.jpg”到-fileURLWithPath:它会返回nil,因为该字符串肯定不是一个文件的路径,这是一个URL字符串

思考的 - fileURLWithPath:正如你预先输入的字符串是“file:// localhost /”...所以你最终会看到一个类似于“file:// localhost/http://www.example.com/wp-content/uploads/image.jpg”的URL。这不好。

如果要传递整个URL字符串,而不仅仅是文件系统路径字符串,则需要调用[NSURL URLWithString:paths]。

+0

由于现在更清晰如果我使用像“http://example.com/name.jpg”这样的url,但是在使用类似“assets-library://asset/asset.JPG?id = E5F41458- 962D-47DD-B5EF-E606E2A8AC7A&ext = JPG“?它是使用ALAsset库建立的,在这种情况下,它总是返回空值 – Hieicker

+0

[NSURL URLWithString:]应该与”assets-library://asset/asset.JPG?id = E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext = JP G“,我会想。它返回零,你是说? –

+0

谢谢!事实上,它并不返回零,但这种错误“ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-11。”它还返回错误“Error loading image”,显然在返回后(总是在日志中)imageSource(null) – Hieicker

1

与“资产库工作:?//asset/asset.JPG ID ..........,试试这个代码的解释

-(UIImage*)resizeImageToMaxSize:(CGFloat)max anImage:(UIImage*)anImage 
{ 

    NSData * imgData = UIImageJPEGRepresentation(anImage, 1); 
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imgData, NULL); 
if (!imageSource) 
    return nil; 

CFDictionaryRef options = (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys: 
              (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, 
              (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent, 
              (id)[NSNumber numberWithFloat:max], (id)kCGImageSourceThumbnailMaxPixelSize, 
              nil]; 
CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options); 

UIImage* scaled = [UIImage imageWithCGImage:imgRef]; 
    //these lines might be skipped for ARC enabled 
CGImageRelease(imgRef); 
CFRelease(imageSource); 

return scaled; }