简体   繁体   English

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

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

How to get the image size of an image file in monotouch without loading it in memory? 如何在单片机中获取图像文件的图像大小而不将其加载到内存中?

I tried to use CGImageSource from the MonoTouch.ImageIO namespace, as indicated in the Apple documentation and in this blog post: 我尝试使用MonoTouch.ImageIO命名空间中的CGImageSource,如Apple文档和此博客文章中所示:

oleb.net oleb.net

But this code doesn't work: 但是这段代码不起作用:

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

Casting to strings doesn't make any difference, the field returns empty. 转换为字符串没有任何区别,字段返回空。

Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

The latest MonoTouch releases makes it a lot easier to get the image properties using the new GetProperties method. 最新的MonoTouch版本使得使用新的GetProperties方法获取图像属性变得更加容易。 Eg 例如

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

Looks like this is a bug in Mono Touch. 看起来这是Mono Touch中的一个错误。 I tested the code in XCode with the same image in Mono Touch and it worked in XCode. 我使用Mono Touch中的相同图像测试了XCode中的代码,并且它在XCode中工作。

In Mono Touch after calling the imageSource.CopyProperties(imageProperties,0) imageProperties.Keys Count is 0 在单触调用后imageSource.CopyProperties(imageProperties,0) imageProperties.Keys计数为0

So you should create a bug report at bugzilla.xamarin.com/ 所以你应该在bugzilla.xamarin.com/上创建一个错误报告

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

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