【问题标题】:CGImageSourceCreateWithURL returns always NULLCGImageSourceCreateWithURL 总是返回 NULL
【发布时间】:2014-02-23 23:27:36
【问题描述】:

我需要在不加载或下载的情况下读取图像的属性。事实上,我已经实现了一个简单的方法,它使用 CGImageSourceCreateWithUrl 来完成这个。 我的问题是它总是返回错误,因为 imageSource 似乎为空。那么我能做些什么来修复它呢? 在 NSURL 对象中,我传递如下网址: “http://www.example.com/wp-content/uploads/image.jpg”还有用于检索手机内图像的 ALAssets 库 ID,例如“assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG”。 这是我的方法:

-(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。

谢谢

【问题讨论】:

    标签: objective-c nsurl cgimagesource


    【解决方案1】:

    如果您将字符串“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]。

    【讨论】:

    • 感谢您的解释。如果我使用像“example.com/name.jpg”这样的网址,现在就更清楚了。但是,如果我使用像“assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG”这样的内部路径,会有什么变化?它是使用 ALAsset 库创建的。在这种情况下,它总是返回 null。
    • [NSURL URLWithString:] 应该与“assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JP‌​G”一起使用,我想。它返回 nil,你是说?
    • 谢谢!事实上,实际上它并没有返回 nil,而是出现了这种错误“ImageIO: CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource failed with error code -11”。它还返回错误“加载图像时出错”,显然在它返回(总是在日志中)imageSource(null)
    【解决方案2】:

    使用“assets-library://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; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2016-11-02
      • 2014-05-06
      • 2012-06-05
      • 2014-05-30
      • 2017-10-12
      • 2013-08-16
      相关资源
      最近更新 更多