[Objective-C Tip] UImage 이미지 리사이징

프로젝트 하실때, UIImage를 많이 사용합니다.
이번 시간에는 UIImage의 관련된 팁을 드리고자 합니다.

* 크기 변경
- (UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height
{
    
    CGImageRef imageRef = [image CGImage];
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    
    //if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;
    
    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImageimageWithCGImage:ref];
    
    CGContextRelease(bitmap);
    CGImageRelease(ref);
    
    return result;    
}

* 이미지 자르기
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0.0, rect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
CGContextClipToRect( currentContext, clippedRect);
CGRect drawRect = CGRectMake(rect.origin.x * -1,rect.origin.y * -1,imageToCrop.size.width,imageToCrop.size.height);
CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
CGContextScaleCTM(currentContext, 1.0, -1.0);
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cropped;
}


* 이미지 마스킹
-(UIImage *)maskingImage:(UIImage *)image maskImage:(NSString)_maskImage{
CGImageRef imageRef = [image CGImage];
CGImageRef maskRef = [[UIImage imageNamed:_maskImage] CGImage];
    
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef),
NULL, false);
    
CGImageRef masked = CGImageCreateWithMask(imageRef, mask);
CGImageRelease(mask);
    
UIImage *maskedImage = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);
return maskedImage;
}


*이미지 합치기
- (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;
}


*UIColor로 IImage를 만들기

 #import <quartzcore/quartzcore.h>
...
+ (UIImage *)image1x1WithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

댓글

주간 인기글

[정보] 인스타그램은 당신의 소리를 '듣고' 있을 수도 있습니다

안드로이드에서 당겨서 새로고침(SwipeRefreshLayout) 쉽게 구현하기

[앱 디자인] 벤치마킹에 유용한 사이트

[앱 디자인] 디자인 가이드 만들기 - 아이폰

[AWS] WinSCP 를 이용해 Linux 인스턴스로 파일 전송하기