如何在不加载图像文件到内存的情况下获取图像文件的图像大小?
我尝试使用MonoTouch.ImageIO命名空间中的CGImageSource,正如苹果文档和这篇博客文章所指出的那样:
oleb.net
但是下面的代码不起作用:
public Size GetImageSizeFromImageFile (string image_file_path)
{
CGImageSource imageSource = CGImageSource.FromUrl (NSUrl.FromFilename (image_file_path));
if (imageSource == null) {
// Error loading image
Console.WriteLine ("Error loading image info for file: " + image_file_path);
return Size.Empty;
}
NSDictionary imageProperties = new NSDictionary ();
imageSource.CopyProperties (imageProperties, 0);
int width = (int)imageProperties.ValueForKey (new NSString ("kCGImagePropertyPixelWidth"));
int height = (int)imageProperties.ValueForKey (new NSString ("kCGImagePropertyPixelHeight"));
Console.WriteLine (@"Image " + image_file_path + " dimensions: " + width + " x " + height+" px");
imageProperties.Dispose ();
return new Size(width, height);
}转换为字符串没有任何区别,该字段返回空。
感谢您的帮助,谢谢!
发布于 2012-05-25 20:59:45
最新的MonoTouch版本使得使用新的GetProperties方法获取图像属性变得更加容易。例如。
using (var src = CGImageSource.FromUrl (NSUrl.FromFilename (filename))) {
CGImageOptions options = new CGImageOptions () { ShouldCache = false };
// do not forget the '0' image index or you won't get what you're looking for!
using (var p = src.GetProperties (options, 0)) {
Console.WriteLine ("{0}x{1}", p.PixelWidth, p.PixelHeight);
}
}发布于 2012-05-25 19:33:00
看起来这是Mono Touch中的一个bug。我用Mono Touch中的相同图像在XCode中测试了代码,它在XCode中工作。
在单色触摸屏中,调用imageSource.CopyProperties(imageProperties,0)后,imageProperties.Keys计数为0
所以你应该在bugzilla.xamarin.com/上创建一个bug报告。
https://stackoverflow.com/questions/10753108
复制相似问题