首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CGImageSourceCreateWithURL总是返回NULL

CGImageSourceCreateWithURL总是返回NULL
EN

Stack Overflow用户
提问于 2014-01-31 10:01:05
回答 2查看 4.5K关注 0票数 3

我需要在没有加载或下载的情况下读取图像的属性。实际上,我已经实现了一个简单的方法,它使用CGImageSourceCreateWithUrl来完成这个任务。我的问题是,它返回总是错误,因为似乎imageSource是空的。那么我能做些什么来解决这个问题呢?在NSURL对象中,我传递诸如:"http://www.example.com/wp-content/uploads/image.jpg“之类的urls,但也传递用于检索手机内部图像的ALAssets库Id,比如http://www.example.com/wp-content/uploads/image.jpg这是我的方法:

代码语言:javascript
运行
复制
-(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是启用的。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2014-01-31 10:49:07

如果要将字符串“http://www.example.com/wp-content/uploads/image.jpg”传递给-fileURLWithPath:它将返回零,因为该字符串肯定不是文件路径,而是一个URL字符串。

想想file://localhost/:在您传入的字符串前加上“file://localhost/http://www.example.com/wp-content/uploads/image.jpg”...so,您最终会得到一个类似于“file://localhost/http://www.example.com/wp-content/uploads/image.jpg”的URL,这并不好。

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

票数 5
EN

Stack Overflow用户

发布于 2014-02-18 06:30:44

使用"assets-library://asset/asset.JPG?id..........,尝试使用以下代码

代码语言:javascript
运行
复制
-(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; }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21477186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档