2
votes

I am taking a image from the Gallery and applying scale, rotate and invert transformations using CGAffineTransform. Finally I am saving the Image to the Gallery. But the saved Image is having white rectangle boundary![Rotated UIImageView with White background][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);
   }

I have added the rotated UIImage in the attachment.

1

1 Answers

0
votes

In order to not have a white background (i.e. an opaque image), you need to use a different function to begin the image context:

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

So, replace:

 UIGraphicsBeginImageContext(rotatedSize);

With:

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

This will make sure that the context is drawn with transparency.