我需要在不加载或下载图像的情况下读取图像的属性。事实上,我已经实现了一个简单的方法,使用 CGImageSourceCreateWithUrl 来完成此任务。
我的问题是它总是返回错误,因为似乎 imageSource 为空。那么我能做些什么来修复它呢?
在 NSURL 对象中,我传递如下网址:
”http://www.example.com/wp-content/uploads/image.jpg 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;
}
我看过这篇文章试图修复它,但似乎没有任何效果:
- CGImageSourceRef imageSource = CGImageSourceCreateWithURL 返回 NULL https://stackoverflow.com/questions/16713610/cgimagesourceref-imagesource-cgimagesourcecreatewithurl-returns-null
- 带有身份验证的 CGImageSourceCreateWithURL https://stackoverflow.com/questions/18852013/cgimagesourcecreatewithurl-with-authentication
- 访问 UIImage 属性而不将图像加载到内存中 https://stackoverflow.com/questions/4169677/accessing-uiimage-properties-without-loading-in-memory-the-image
在我的项目中启用了 ARC。
Thanks
如果您传递字符串“http://www.example.com/wp-content/uploads/image.jpg http://www.example.com/wp-content/uploads/image.jpg” to -fileURLWithPath:它将返回 nil,因为该字符串肯定不是文件路径,而是 URL 字符串。
将 -fileURLWithPath: 想象为在您传入的字符串前面添加“file://localhost/”...这样您最终会得到一个类似于“file://localhost/”的 URLhttp://www.example.com/wp-content/uploads/image.jpg http://www.example.com/wp-content/uploads/image.jpg”。这不好。
如果要传递整个 URL 字符串,而不仅仅是文件系统路径字符串,则需要调用 [NSURL URLWithString:paths]。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)