【问题标题】:Resize and saving an image to disk in Monotouch在 Monotouch 中调整图像大小并将其保存到磁盘
【发布时间】:2011-01-25 22:54:10
【问题描述】:

我正在尝试调整从磁盘加载的图像的大小 - JPG 或 PNG(我不知道加载时的格式) - 然后将其保存回磁盘

我有以下代码,我试图从 Objective-c 移植,但是我卡在了最后一部分。 Original Objective-C.

这可能不是实现我想做的最好的方式 - 任何解决方案都适合我。

int width = 100;
int height = 100;

using (UIImage image = UIImage.FromFile(filePath))
{
    CGImage cgimage = image.CGImage;
    CGImageAlphaInfo alphaInfo = cgimage.AlphaInfo;

    if (alphaInfo == CGImageAlphaInfo.None)
        alphaInfo = CGImageAlphaInfo.NoneSkipLast;

    CGBitmapContext context = new CGBitmapContext(IntPtr.Zero,
        width,
        height,
        cgimage.BitsPerComponent,
        4 * width,
        cgimage.ColorSpace,
        alphaInfo);

    context.DrawImage(new RectangleF(0, 0, width, height), cgimage);

    /*
    Not sure how to convert this part:

    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);
    */
}

【问题讨论】:

    标签: objective-c resize uiimage xamarin.ios


    【解决方案1】:

    要从上下文中获取 UIImage,您需要做的就是:

    UIImage* result = UIImage.FromImage(context.ToImage());
    

    【讨论】:

      【解决方案2】:
          public static UIImage EditPhoto(int iMode, UIImage origImg)
          {
              SizeF newSize;
      
              if (iMode == 1 || iMode == 2)
                  newSize = new SizeF(origImg.Size.Height, origImg.Size.Width);
              else
                  newSize = origImg.Size;
      
              UIGraphics.BeginImageContext(newSize);
              CGContext ctx = UIGraphics.GetCurrentContext();
      
              switch (iMode)
              {
                  case 1:         // Rotate counter-clockwise 90 degrees
                      ctx.TranslateCTM(origImg.Size.Height, origImg.Size.Width);
                      ctx.ScaleCTM(1f, -1f);
                      ctx.RotateCTM(1.57079633f);     // angle is in radians
                      break;
      
                  case 2:         // Rotate clockwise 90 degrees
                      ctx.ScaleCTM(1f, -1f);
                      ctx.RotateCTM(-1.57079633f);     // angle is in radians
                      break;
      
                  case 3:         // Flip vertical
                      // Do nothing. The image comes out flipped vertically because Core Graphics / OpenTK uses cartesian coordinates
                      break;
      
                  case 4:         // Flip horizontal
                      ctx.TranslateCTM(newSize.Width, newSize.Height);
                      ctx.ScaleCTM(-1f, -1f);
                      break;
      
                  default:        // Return unchanged image
                      ctx.TranslateCTM(0, origImg.Size.Height);
                      ctx.ScaleCTM(1f, -1f);
                      break;
              }
      
              ctx.DrawImage(new RectangleF(0, 0, origImg.Size.Width, origImg.Size.Height), origImg.CGImage);
              UIImage resImg = UIGraphics.GetImageFromCurrentImageContext();
      
              UIGraphics.EndImageContext();
              return resImg;
          }
      

      【讨论】:

        【解决方案3】:

        在即将到来的 MonoTouch 中,我们将有一个 scale 方法,这是它在 UIImage.cs 中的实现:

            public UIImage Scale (SizeF newSize)
            {
                UIGraphics.BeginImageContext (newSize);
                var context = UIGraphics.GetCurrentContext ();
                context.TranslateCTM (0, newSize.Height);
                context.ScaleCTM (1f, -1f);
        
                context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), CGImage);
        
                var scaledImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
        
                return scaledImage;         
            }
        

        调整为在 MonoTouch 之外重复使用:

            public static UIImage Scale (UIImage source, SizeF newSize)
            {
                UIGraphics.BeginImageContext (newSize);
                var context = UIGraphics.GetCurrentContext ();
                context.TranslateCTM (0, newSize.Height);
                context.ScaleCTM (1f, -1f);
        
                context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), source.CGImage);
        
                var scaledImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
        
                return scaledImage;         
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-10
          • 1970-01-01
          • 1970-01-01
          • 2011-11-16
          相关资源
          最近更新 更多