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