CGImageSourceCreateWithURL 始终返回 NULL [英] CGImageSourceCreateWithURL returns always NULL

查看:122
本文介绍了CGImageSourceCreateWithURL 始终返回 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不加载或下载的情况下读取图像的属性.事实上,我已经实现了一个简单的方法,它使用 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".这是我的方法:

I need to read the properties of an image without load or download it. In fact i have implemented a simple method that use the CGImageSourceCreateWithUrl for accomplish this. My problem is that it is returning always error because seems that the imageSource is null. So what can i do for fix it? In the NSURL object i pass urls like: "http://www.example.com/wp-content/uploads/image.jpg" but also ALAssets library Id used to retrieve images inside the phone like "assets-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG". This is my method:

-(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;
}

我已经看到这篇文章试图修复它,但似乎没有任何效果:

I have saw this posts for try to fix it but nothing seems working:

在我的项目中启用了 ARC.

In my project ARC is enabled.

谢谢

推荐答案

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

If you are passing the string "http://www.example.com/wp-content/uploads/image.jpg" to -fileURLWithPath: it’s going to return nil, because that string is sure not a file path, it’s a URL string.

想想 -fileURLWithPath:就像在你传入的字符串前面加上file://localhost/"...所以你最终会得到一个看起来像file://localhost/http://www.example.com/wp-content/uploads/image.jpg".那不好.

Think of -fileURLWithPath: as just prepending the string you pass in with "file://localhost/"...so you’d end up with a URL that looks like "file://localhost/http://www.example.com/wp-content/uploads/image.jpg". That’s not good.

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

You need to call [NSURL URLWithString:paths] if you’re going to be passing in entire URL strings, not just a filesystem path strings.

这篇关于CGImageSourceCreateWithURL 始终返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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