맨날 UIImage 만 사용하다보니.. 아는게 없다. -_-;
- CGImageRef Image Orientation
UIGraphicsBeginImageContext 에 CGContextRef를 이용하는 방법이 있긴하나 소스가 좀 길어지고...암튼 아직 좀 적응이 안된다..그래서 간단한 방법으로 CGImageRef를 이용하는 방법을 찾았다.
for( int i = 0; i < 8; i++)
{
CGImageRef imageRef = CGImageCreateCopy([UIImage imageNamed:@"Icon.png"].CGImage);
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * 60 + 20, 20, 57, 57)];
imageView.image = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:i];//UIImageOrientationDown
CGImageRelease(imageRef);
[self.view addSubview:imageView];
}UIImageOrientation 은 UIImage.h 헤더에 있다.
typedef enum {
UIImageOrientationUp, // default orientation
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CCW
UIImageOrientationRight, // 90 deg CW
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // vertical flip
UIImageOrientationRightMirrored, // vertical flip
} UIImageOrientation;enum 이니 걍 for을 이용해서 다 돌려 봤더니..
아래 화면 처럼 이미지가 회전되어 그려지는 걸 볼 수 있다.
- Image Mask
UIGraphicsGetCurrentContext() 를 이용하여 2개의 이미지를 하나로 만든다.
-(UIImage *)maskingImage:(UIImage *)image1 makskImage:(UIImage*)image2 toPoint:(CGPoint)point {
UIGraphicsBeginImageContext(image1.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0.0, image1.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawImage(currentContext, CGRectMake(0, 0, image1.size.width, image1.size.height), image1.CGImage);
CGRect drawRect = CGRectMake(point.x, (image1.size.height - image2.size.height) - point.y, image2.size.width, image2.size.height);
CGContextDrawImage(currentContext, drawRect, image2.CGImage);
UIImage* maskedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return maskedImage;
}
액자와 같은 이미지 위에 이미지를 올렸다..
- Image Resize
-(UIImage *)resizeImage:(UIImage *)image width:(float)resizeWidth height:(float)resizeHeight{
UIGraphicsBeginImageContext(CGSizeMake(resizeWidth, resizeHeight));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, resizeHeight);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, resizeWidth, resizeHeight), [image CGImage]);
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
왼쪽 이미지가 resizeImage:width:height: 메소드를 이용한 것 이고,
오른쪽이미지가 UIImageView의 frame의 크기를 늘린 것 이다.
57 * 57 크기의 이미지를 114 * 114로 출력한 것 인데..
자세히 보면 왼쪽 이미지가 좀 더 선명한 느낌이다.
- Image Cropping
- (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;
}
'Program > iOS' 카테고리의 다른 글
Mac - Inactive memory can be slow to free up (1) | 2011.09.26 |
---|---|
iOS mpmovieplayercontroller (4) | 2011.08.31 |
iOS - OTA ( Over the Air AdHoc ) 2 (2) | 2011.08.25 |
iOS - 인증되지 않은 다른 맥에서 개발하기 (4) | 2011.08.22 |
iOS - OTA ( Over the Air AdHoc ) 1 (6) | 2011.08.19 |