English 中文(简体)
如何在不装入图像文件的情况下, 将图像文件的图像大小放在单调图纸中?
原标题:How to get the image size of an image file in monotouch without loading it?

如何获得图像文件的图像大小, 而不装入内存?

我试图使用来自Monotouch. ImagieIO 命名空间的CGIMAGE来源,

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 发布让使用新的 < code> GetPropertys 方法获取图像属性更容易得多。 例如 。

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

看起来像是单触法中的窃听器。 我用相同的图像测试了XCode中的代码, 而在XCode中起作用了。

调用 image source.coppyPropertys (imagePropertys,0) image propertys.Keys 计数为 0

因此,您应该在bugzilla.xamarin.com/ 创建错误报告





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签