繁体   English   中英

如何在单片机中获取图像文件的图像大小而不加载它?

[英]How to get the image size of an image file in monotouch without loading it?

如何在单片机中获取图像文件的图像大小而不将其加载到内存中?

我尝试使用MonoTouch.ImageIO命名空间中的CGImageSource,如Apple文档和此博客文章中所示:

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);
    }

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

任何帮助表示赞赏,谢谢!

最新的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);
    }
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM