카메라가 landscapeRight인 경우를 기술
사용할때는
image = [self rotateImage:image byOrientationFlag:[[UIDevice currentDevice] orientation]];
위와 같이 사용하면 된다.
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
- (UIImage *)rotateImage:(UIImage *)img byOrientationFlag:(UIDeviceOrientation)orient
{
NSLog(@"ImageProcessUtil rotateImage");
CGImageRef imgRef = img.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
CGFloat boundHeight;
NSLog(@"rotate image size width=%f, height=%f, orientation=%d", width, height, orient);
switch(orient) {
case UIDeviceOrientationPortrait:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
break;
case UIDeviceOrientationPortraitUpsideDown:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * bounds.size.width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(nil, bounds.size.width, bounds.size.height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
if (orient == UIDeviceOrientationLandscapeLeft)
{
// nothing
}
else if (orient == UIDeviceOrientationLandscapeRight)
{
CGContextRotateCTM (context, DEGREES_TO_RADIANS(180));
CGContextTranslateCTM (context, -width, -height);
}
else if (orient == UIDeviceOrientationPortraitUpsideDown)
{
CGContextRotateCTM (context, DEGREES_TO_RADIANS(90));
CGContextTranslateCTM (context, 0, -height);
}
else if (orient == UIDeviceOrientationPortrait) {
CGContextRotateCTM (context, DEGREES_TO_RADIANS(270));
CGContextTranslateCTM (context, -width, 0);
}
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *imageCopy = [UIImage imageWithCGImage:newImage];
CFRelease(newImage);
return imageCopy;
}
요새 portrait보단 Landscape쪽으로 카메라를 바꾸는 중이라다시 써본다.
사실 예전 포스팅인 http://life-shelter.tistory.com/78
위 포스팅은 portrait카메라인 경우였다.
'프로그래밍 > iOS' 카테고리의 다른 글
[iOS] UIImage를 특정 rect만큼 자르기 (0) | 2015.09.09 |
---|---|
[iOS] UI 회전시키기 (0) | 2015.08.17 |
[iOS] 시간 측정하기 (0) | 2015.08.10 |
[iOS] 이어폰 plug in/out 이벤트 받아오기 (0) | 2015.07.14 |
[ios] 베타(beta)버전 올리기 (0) | 2015.07.09 |