2015-01-08 24 views
1

이 문제로 저를 죽입니다. 나는 무엇이 잘못 될지 모른다. 그러나 다음 코드는 이미지를 거꾸로 뒤집습니다. 사실 수직 플립을하고 왜 그런지 모르겠습니다.CGContextRef => CGImageRef => CIImage => CGImageRef => CGContextDrawImage 체인이 이미지를 거꾸로 뒤집습니다.

UIFont *font = [UIFont fontWithName:fontName size:fontSize]; 

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; 
[attributes setObject:font forKey:NSFontAttributeName]; 
[attributes setObject:[NSNumber numberWithFloat:kStrokeWidth] forKey:NSStrokeWidthAttributeName]; 
[attributes setObject:[UIColor redColor] forKey:NSStrokeColorAttributeName]; 
[attributes setObject:style forKey:NSParagraphStyleAttributeName]; 

[text drawInRect:drawRect withAttributes:attributes]; 

[attributes removeObjectForKey:NSStrokeWidthAttributeName]; 
[attributes removeObjectForKey:NSStrokeColorAttributeName]; 
[attributes setObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName]; 
[text drawInRect:drawRect withAttributes:attributes]; 

CGImageRef cgImg = CGBitmapContextCreateImage(context); 
CIImage *beginImage = [CIImage imageWithCGImage:cgImg]; 

CIContext *cicontext = [CIContext contextWithOptions:nil]; 
CGImageRef cgimg = [cicontext createCGImage:beginImage fromRect:[beginImage extent]]; 
CGContextDrawImage(context, [beginImage extent] , cgimg); 
CGImageRelease(cgImg); 
CGImageRelease(cgimg); 


UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

이것은이 발생합니다 :

enter image description here

왜, 오, 왜?

+0

당신이 회전을 잃고 왜 당신은 또한 만드는 CIContext – Andrea

+0

나는 CIContet 렌더링을 만들 수 있습니다 : 내가 그리는 동안 당신이 수직으로 초기 CGContextRef 플립하는 것이 좋습니다? CIImage를 CGImageRef로 되 돌린다. 무슨 회전 말이에요? – Teddy

답변

2

일 수 있습니다. 대답보다 답글로 더 적합하지만 의견이 너무 길습니다.

@Andrea가 지적했듯이 CGContext와 CIContext를 모두 생성하는 것은 약간 이상합니다. 당신은 단지 CGImageRef에서있는 UIImage를 추출 할 경우 당신은 여전히 ​​이 newImage 것이다

UIImage *newImage = [[UIImage alloc] initWithCGImage:cgImg] 

를 뒤집 결과적를 사용할 수 있습니다. UIImages 및 CGContextRefs에서 사용하는 좌표계는 반대 방향으로 향한 수직 축이 있습니다. 그 방법은 호출되는 상황을 만들 때

은 아마
CGContextSaveGState(context); 
CGContextTranslateCTM(context, 0, CGBitmapContextGetHeight(context)); 
CGContextScaleCTM(context, 1, -1); 
// All your drawing code goes here. 
CGContextRestoreGState(context); 
+0

감사합니다. 좋은 설명입니다. 나는이 좌표 문제에 대해 몰랐다. 그것은 그 문제를 설명 할 수 있습니다. 그러나 두 가지 방법 (CIImage와의 렌더링은 두 번 플립을 수행해야합니다)이 발생하지 않는 이유는 다소 혼란 스럽습니다. 이미지를 CIImage로 변환하는 이유는 earleir를 CGImage로 렌더링 한 텍스트에 흐림 필터를 적용하는 것입니다. – Teddy

+0

@Teddy Aaron이 맞습니다. iOS에서 원점은 왼쪽 상단, 왼쪽 하단은 Mac, CG는 osx에서 오기 때문에 코어 그래픽의 좌표계가 뒤집 깁니다. 이미 뒤집힌 CGContext를 만드는 UIKit에도 API가 있습니다. – Andrea

+0

. 고마워 – Teddy