[英]How to get the image size of an image file in monotouch without loading it?
如何在單片機中獲取圖像文件的圖像大小而不將其加載到內存中?
我嘗試使用MonoTouch.ImageIO命名空間中的CGImageSource,如Apple文檔和此博客文章中所示:
但是這段代碼不起作用:
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.