2013-12-11 3 views
2

갤러리에서 이미지를 가져 와서 스케일을 적용하고 CGAffineTransform을 사용하여 변환을 회전 및 반전합니다. 마지막으로 이미지를 갤러리에 저장합니다. 그러나 저장된 이미지는 흰색 직사각형 경계를가집니다! [회전 된 UIImageView with White background] [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]) 

이렇게하면 컨텍스트가 투명도로 그려집니다.

관련 문제