iOS 개발 하다 보니 image를 crop 하거나 resize를 해야 하는 경우가 많더구요.
구글링 해보니 괜찮게 만들어 놓은 소스도 많아서 참고해서 UIImage에 Category로 만들어서 사용하고 있습니다.
- @implementation UIImage ( NBUIImageCrop )
- - (UIImage *)crop:(CGRect)rect {
- if (self.scale > 1.0f) {
- rect = CGRectMake(rect.origin.x * self.scale,
- rect.origin.y * self.scale,
- rect.size.width * self.scale,
- rect.size.height * self.scale);
- }
- if ([self imageOrientation] == UIImageOrientationLeft) {
- rect.size = CGSizeMake(rect.size.height, rect.size.width);
- rect.size = CGSizeMake(rect.size.height, rect.size.width);
- rect.origin = CGPointMake(rect.origin.y, rect.origin.x);
- }
- else if ([self imageOrientation] == UIImageOrientationRight) {
- rect.size = CGSizeMake(rect.size.height, rect.size.width);
- rect.origin = CGPointMake(rect.origin.y, rect.origin.x);
- }
- CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
- UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
- CGImageRelease(imageRef);
- return result;
- }
- - (UIImage *)resizeToFitWidth:(CGFloat)width
- {
- CGSize size = CGSizeMake(width, self.size.height * width/self.size.width);
- UIGraphicsBeginImageContext(size);
- [self drawInRect:CGRectMake(0,0,size.width,size.height)];
- UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
'iOS' 카테고리의 다른 글
#iOS RoundRectedImageView 만들기. (0) | 2012.09.13 |
---|---|
[IPhone] UITextView를 UITextField의 placeholder 처럼 사용하기 (0) | 2012.09.12 |
[iOS] NSDate 를 이용한 파일 이름 생성 (0) | 2012.09.09 |
Iphone- Make a UITextView move up when keyboard is present (0) | 2012.09.04 |
navigationBar 이미지 설정하기와 iOS5, iOS4 (0) | 2012.09.04 |