簡體   English   中英

如何從iOS中旋轉,縮放和倒置的UIImageView中提取UIImage

[英]How to extract the UIImage from rotated, scaled and inverted UIImageView in iOS

我正在從圖庫中拍攝圖像,並使用CGAffineTransform應用縮放,旋轉和反轉轉換。 最后,我將圖像保存到圖庫。 但是,已保存的圖像具有白色矩形邊界![具有白色背景的旋轉UIImageView] [1]。

- (UIImage *)getTheTransformedOriginalImage:(UIImage *)aTransImage
  {
    CGAffineTransform transform = selImageView.transform;
   CGPathRef rotatedImageRectPath = CGPathCreateWithRect(selImageView.frame, &transform);
    CGRect boundingBox = CGPathGetBoundingBox(rotatedImageRectPath);

   CGSize rotatedSize = boundingBox.size;

   UIGraphicsBeginImageContext(rotatedSize);
   UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 0.0f);

// Move the origin to the middle of the image so we will rotate and scale around the center.
  CGContextTranslateCTM(UIGraphicsGetCurrentContext(), rotatedSize.width/2,    rotatedSize.height/2);

//Rotate the image context using tranform
   CGContextConcatCTM(UIGraphicsGetCurrentContext(), selImageView.transform);
// Now, draw the rotated/scaled image into the context
   CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
   CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(-aTransImage.size.width / 2, -aTransImage.size.height / 2, aTransImage.size.width, aTransImage.size.height), [aTransImage CGImage]);

  UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return newImage;
}

- (IBAction)saveToGalleryClicked:(id)sender
  {
      UIImage *aTransImage = selImageView.image;
      UIImage *shareImage = [self getTheTransformedOriginalImage:aTransImage]
      UIImageWriteToSavedPhotosAlbum(shareImage, nil, nil, nil);
   }

我在附件中添加了旋轉的UIImage。

為了沒有白色背景(即不透明圖像),您需要使用其他函數來開始圖像上下文:

 UIGraphicsBeginImageContextWithOptions(CGSize size, **BOOL opaque**, CGFloat scale) 

因此,請替換:

 UIGraphicsBeginImageContext(rotatedSize);

帶有:

 UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen]scale])

這樣可以確保上下文透明地繪制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM