2017-06-23 8 views
0

iOS-Appでこれまでと同じように、自分のMac-Appで画像のサイズを変更しようとしています。問題は、NSImageにUIImageが行うすべてのメソッドが含まれていないことです。ここでiOSで使用したコードはXamarin Mac - サイズ変更Image

public static UIImage MaxResizeImage(this UIImage sourceImage, float maxWidth, float maxHeight) 
    { 
     var sourceSize = sourceImage.Size; 
     var maxResizeFactor = Math.Max(maxWidth/sourceSize.Width, maxHeight/sourceSize.Height); 
     if (maxResizeFactor > 1) return sourceImage; 
     float width = (float)(maxResizeFactor * sourceSize.Width); 
     float height = (float)(maxResizeFactor * sourceSize.Height); 
     UIGraphics.BeginImageContext(new SizeF(width, height)); 
     sourceImage.Draw(new RectangleF(0, 0, width, height)); 
     var resultImage = UIGraphics.GetImageFromCurrentImageContext(); 
     UIGraphics.EndImageContext(); 
     return resultImage; 
    } 

Mac-Appの場合はどのように書き直すことができますか?あなたの助けをありがとう

+0

解決しない場合は、anwserを受け入れるかコメントを追加してください – svn

答えて

0

私は私のMacアプリケーションのための働く機能を持って、あなたの機能に合うように書き換えました。このバージョンはテストしていませんが、うまくいくはずです。

public static NSImage MaxResizeImage(this NSImage sourceImage, float maxWidth, float maxHeight) 
{ 
    var sourceSize = sourceImage.Size; 
    var maxResizeFactor = Math.Max(maxWidth/sourceSize.Width, maxHeight/sourceSize.Height); 
    if (maxResizeFactor > 1) return sourceImage; 
    float width = (float)(maxResizeFactor * sourceSize.Width); 
    float height = (float)(maxResizeFactor * sourceSize.Height); 
    var targetRect = new CoreGraphics.CGRect(0,0,width, height); 
    var newImage = new NSImage (new CoreGraphics.CGSize (width, height)); 
    newImage.LockFocus(); 
    sourceImage.DrawInRect(targetRect, CoreGraphics.CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f); 
    newImage.UnlockFocus(); 

    return newImage; 
} 
関連する問題