2013-12-11 46 views
2

我從Gallery中獲取圖像,並使用CGAffineTransform應用縮放,旋轉和反轉轉換。最後,我將圖像保存到圖庫中。但保存的圖像有白色的矩形邊界![帶有白色背景的旋轉UIImageView] [1]。如何從iOS中的旋轉,縮放和倒置的UIImageView中提取UIImage

- (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。

回答

0

爲了不具有白色背景(即不透明圖像),你需要使用不同的功能,開始圖像內容:

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

所以,替換:

UIGraphicsBeginImageContext(rotatedSize); 

隨着:

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

這將確保上下文繪製透明。

相關問題