2017-01-03 116 views
1

畫我有幾個UIImageView並在其圖層屬性應用於CATransform3D,現在我希望得到一個單一的圖像(與改造相結合的所有圖片一)。上UIImage的應用CATransform3D而上下文

對於我使用CoreGraphics在框架上繪製背景圖片,然後得到最終圖像。

需要實現這一點:

  • 畫每幅圖像背景與CATransform3D,然後得到圖像內容的圖像。

我試過,但沒有得到成功。

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextConcatCTM(context, CATransform3DGetAffineTransform(imageView.layer.transform)); 
CGContextDrawImage(context, (CGRect){ CGPointZero, imageView.image.size }, [imageView.image CGImage]); 
CGContextTranslateCTM(context, 0, imageView.frame.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 

我也試過CoreImage框架,但仍然沒有運氣。 :(

CIImage *ciImage = [CIImage imageWithData:UIImagePNGRepresentation(imageView.image)]; 
CIContext *context = [CIContext contextWithOptions:nil]; 
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeRectangle context:context options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}]; 

CIRectangleFeature *rect = (CIRectangleFeature *)[detector featuresInImage:ciImage options:@{CIDetectorImageOrientation : [NSNumber numberWithInt:1]}].firstObject; 

CIFilter *filterMain = [CIFilter filterWithName:@"CIPerspectiveCorrection"]; 
[filterMain setValue:[CIVector vectorWithCGPoint:rect.topLeft] forKey:@"inputTopLeft"]; 
[filterMain setValue:[CIVector vectorWithCGPoint:rect.topRight] forKey:@"inputTopRight"]; 
[filterMain setValue:[CIVector vectorWithCGPoint:rect.bottomRight] forKey:@"inputBottomRight"]; 
[filterMain setValue:[CIVector vectorWithCGPoint:rect.bottomLeft] forKey:@"inputBottomLeft"]; 
[filterMain setValue:sourceCIImage forKey:kCIInputImageKey]; 

CIImage *outputImage = [filterMain valueForKey:kCIOutputImageKey]; 
UIImage *resultImage = [UIImage imageWithCIImage:outputImage]; 

的有關的代碼行給了我一些奇怪的結果。

,我試過的另一件事是drawViewHierarchyInRect:afterScreenUpdates:方法,但它給了我非常模糊的圖像。

因此,如何能我實現與CATransform3D背景繪製圖像,如果有人都面臨着這樣的問題,並修復它,請分享你的想法任何幫助將不勝感激

注:。我不是在談論CGAffineTransform通貨膨脹的CATransform3D

回答

2

蘋果已經提供了一個稱爲CoreImage框架,您可以通過應用過濾得到變換後的圖像。您可以使用CIPerspectiveTransform過濾器通過傳遞圖像/圖像視圖的四個角(TopLeft,TopRight,BottomLeft和BottomRight)來獲取轉換後的圖像。我已經回答了這類問題here...