首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不加载图片文件的情况下获取图片文件的图片大小?

如何在不加载图片文件的情况下获取图片文件的图片大小?
EN

Stack Overflow用户
提问于 2012-05-25 18:50:07
回答 2查看 1.3K关注 0票数 3

如何在不加载图像文件到内存的情况下获取图像文件的图像大小?

我尝试使用MonoTouch.ImageIO命名空间中的CGImageSource,正如苹果文档和这篇博客文章所指出的那样:

oleb.net

但是下面的代码不起作用:

代码语言:javascript
复制
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);
    }

转换为字符串没有任何区别,该字段返回空。

感谢您的帮助,谢谢!

EN

回答 2

Stack Overflow用户

发布于 2012-05-25 20:59:45

最新的MonoTouch版本使得使用新的GetProperties方法获取图像属性变得更加容易。例如。

代码语言:javascript
复制
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);
    }
}
票数 4
EN

Stack Overflow用户

发布于 2012-05-25 19:33:00

看起来这是Mono Touch中的一个bug。我用Mono Touch中的相同图像在XCode中测试了代码,它在XCode中工作。

在单色触摸屏中,调用imageSource.CopyProperties(imageProperties,0)后,imageProperties.Keys计数为0

所以你应该在bugzilla.xamarin.com/上创建一个bug报告。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10753108

复制
相关文章

相似问题

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