Objective c CGImageSourceCreateWithURL始终返回NULL
我需要在不加载或下载的情况下读取图像的属性。事实上,我已经实现了一个简单的方法,它使用CGImageSourceCreateWithUrl来实现这一点。 我的问题是它总是返回错误,因为imageSource似乎为空。那么我能做些什么来修复它呢? 在NSURL对象中,我传递URL,如下所示: “”还设置了用于检索手机内图像的库Id,如“资产”-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG”。 这是我的方法:Objective c CGImageSourceCreateWithURL始终返回NULL,objective-c,nsurl,cgimagesource,Objective C,Nsurl,Cgimagesource,我需要在不加载或下载的情况下读取图像的属性。事实上,我已经实现了一个简单的方法,它使用CGImageSourceCreateWithUrl来实现这一点。 我的问题是它总是返回错误,因为imageSource似乎为空。那么我能做些什么来修复它呢? 在NSURL对象中,我传递URL,如下所示: “”还设置了用于检索手机内图像的库Id,如“资产”-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;
}
我看到这篇文章试图修复它,但似乎没有任何效果:
如果要将字符串“”传递给-fileURLWithPath,请多谢:它将返回nil,因为该字符串肯定不是文件路径,而是URL字符串 将-fileURLWithPath:看作是在传入的字符串前面加上前缀“file://localhost/“…所以你最终会得到一个类似”file://localhost/”“那不好 如果要传递整个URL字符串,而不仅仅是文件系统路径字符串,则需要调用[NSURL URLWithString:path]-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; }
谢谢你的解释。如果我使用像“”这样的url,现在就更清楚了。但是如果我使用像“资产”这样的内部路径,会有什么变化呢-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JPG“”?它是使用ALAsset库创建的。在这种情况下,它始终返回null。[NSURL URLWithString:]应与“”一起使用资产-library://asset/asset.JPG?id=E5F41458-962D-47DD-B5EF-E606E2A8AC7A&ext=JP“G”,我想。它返回零,你是说?谢谢!事实上,它并没有返回nil,但这类错误“ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-11”。它还返回错误“错误加载图像”,显然在返回(始终在日志中)imageSource(null)后返回