iDev

[iOS Dev] Image&String 등을 Image로 합치기

KraZYeom 2011. 7. 31. 12:18
반응형
주석이 곧 설명.

- (UIImage*)makeImage{

    // 기본 이미지 

    UIImage *image = [UIImage imageNamed:@"image.png"]; 

    // 사용할 글자

    NSString *string = [NSString stringWithFormat:@"%@", @"Steve"];

    CGFloat fontSize = 15;

    UIFont *font = [UIFont systemFontOfSize:fontSize];

    CGSize textSize = [string sizeWithFont:font];

    

    // 반환될 이미지 크기 

    CGSize resultImageSize = CGSizeMake(image.size.width + textSize.width, image.size.height);

    

    // Creates a bitmap-based graphics context and makes it the current context.

    UIGraphicsBeginImageContext(resultImageSize);

    

    // Returns the current graphics context.

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    

    // 이미지 시작점

    CGPoint pt = CGPointZero;

[image drawAtPoint:pt];

    

    // Sets the current fill color to a value in the DeviceRGB color space.

    CGContextSetRGBFillColor(contextRef, 1.0, 0.0, 0.0, 1.0);

    

    CGPoint textPt = CGPointMake(image.size.width, 0);

    [string drawAtPoint:textPt withFont:font];

    

    // Returns an image based on the contents of the current bitmap-based graphics context.

    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

    // Removes the current bitmap-based graphics context from the top of the stack.

    UIGraphicsEndImageContext();

    

    return resultImage;

}


실행을 하면 아래와 같이 Image 로 반환이 됩니다. 


반응형