3

メモリにロードせずにモノタッチで画像ファイルの画像サイズを取得する方法は?

Apple のドキュメントとこのブログ投稿に示されているように、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);
    }

文字列にキャストしても違いはありません。フィールドは空を返します。

どんな助けでも大歓迎です、ありがとう!

4

2 に答える 2

4

GetProperties最新の MonoTouch リリースでは、新しい方法を使用して画像のプロパティを取得することが非常に簡単になりました。例えば

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-25T12:59:44.947 に答える
0

これは Mono Touch のバグのようです。Mono Touch で同じ画像を使用して XCode でコードをテストしたところ、XCode で動作しました。

コール後のモノタッチでimageSource.CopyProperties(imageProperties,0) imageProperties.Keysカウントが0

そのため、bugzilla.xamarin.com/ でバグ レポートを作成する必要があります。

于 2012-05-25T11:32:59.720 に答える