반응형
프로젝트 하실때, 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;
}
'Programming > iOS' 카테고리의 다른 글
[ xcode Tip] 하드용량 확보하기 (0) | 2020.04.17 |
---|---|
[Objective-C Tip] Key-Value Observing (0) | 2020.04.17 |
[Objective-C Tip] 메모리 관리 (0) | 2020.04.17 |
[Objective-C Tip] 디클레어 프로퍼티 (0) | 2020.04.17 |
[iOS 강좌] 오픈소스로 쉽게 카메라 앱 만들기 - 3부 (필터) (1) | 2020.04.16 |