2012-10-19 4 views
0

왜 CGContextDrawImage.Thanks를 호출하여 이미지가 회전되었는지 설명합니다.CGContextDrawImage를 호출하여 이미지가 회전하는 이유

// Initialization code 
UIImage *img = [UIImage imageNamed:@"logo.png"]; 
UIImagePNGRepresentation(img); 
_image_ref = img.CGImage; 

// Drawing code 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGRect img_rect = CGRectMake(20, 40, 100, 150); 
CGContextDrawImage(context, img_rect, _image_ref); 

답변

2

UIKit이 아닌 코어 그래픽의 조정 시스템 인 경우 올바른 좌표를 계산해야합니다. http://blog.ddg.com/?p=10

+0

멋지다. 고마워. – JCN

0

다음은 this 설명입니다. 하나의 상황에서 사용자 정의 rect을 사용하여 여러 이미지를 그릴 수있는 솔루션을 만들었습니다. 이미지와

func foo() -> UIImage? { 
    let image = UIImage(named: "back.png")! 

    let contextSize = CGSize(width: 500, height: 500) 
    UIGraphicsBeginImageContextWithOptions(contextSize, true, image.scale) 
    guard let ctx = UIGraphicsGetCurrentContext() else { return nil } 
    guard let cgImage = image.cgImage else { return nil} 

    //Start code which can by copy/paste 
    let imageRect = CGRect(origin: CGPoint(x: 200.0, y: 200.0), size: image.size) //custom rect 
    let ty = imageRect.origin.y + imageRect.size.height //calculate translation Y 
    let imageRectWithoutOriginY = CGRect(origin: CGPoint(x: imageRect.origin.x, y: 0), size: imageRect.size) 
    ctx.translateBy(x: 0.0, y: ty) //prepare context for custom rect 
    ctx.scaleBy(x: 1.0, y: -1.0) 

    ctx.draw(cgImage, in: imageRectWithoutOriginY) //draw image 

    ctx.translateBy(x: 0.0, y:-ty) //restore default context setup (so you can select new area to place another image) 
    ctx.scaleBy(x: 1.0, y: -1.0) 
    //End code which can by copy/paste 

    let result = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return result 
} 

예 :

enter image description here

나는 그것을 리팩토링 될 수 있음을 알고있다. 더 명확하게 코드를 복제했습니다.

관련 문제