이미지 orientation이 up이 아닌경우 up으로 맞춰주기 위해 이미지 회전함수를 쓰는데
그것을 기술
- ObjC
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
- (UIImage *)rotateImage:(UIImage *)img byOrientationFlag:(UIImageOrientation)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 UIImageOrientationUp:
break;
case UIImageOrientationDown:
break;
case UIImageOrientationLeft:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
break;
case UIImageOrientationRight:
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
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 == UIImageOrientationRight) {
CGContextRotateCTM (context, DEGREES_TO_RADIANS(270));
CGContextTranslateCTM (context, -width, 0);
}
else if (orient == UIImageOrientationLeft) {
CGContextRotateCTM (context, DEGREES_TO_RADIANS(90));
CGContextTranslateCTM (context, 0, -height);
}
else if (orient == UIImageOrientationDown) {
CGContextRotateCTM (context, DEGREES_TO_RADIANS(180));
CGContextTranslateCTM (context, -width, -height);
}
else if (orient == UIImageOrientationUp) {
// NOTHING
}
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *imageCopy = [UIImage imageWithCGImage:newImage];
CFRelease(newImage);
return imageCopy;
}
- swift3
/**
각도를 radian으로 바꿔주는 함수
- parameter angle : 각도
- returns: CGFloat : radian값
*/
func DEGREES_TO_RADIANS(_ angle:CGFloat) -> CGFloat
{
return angle / 180.0 * .pi
}
/**
이미지를 imageOrientation에 따라 up으로 돌려주는 함수
- parameter img : 원본 이미지
- parameter orient : image orientation
- returns: UIImage : 돌려진 UIImage
*/
func rotateImage(img:UIImage, byOrientationFlag orient:UIImageOrientation) ->UIImage
{
let imgRef = img.cgImage
let width:CGFloat = CGFloat((imgRef?.width)!)
let height:CGFloat = CGFloat((imgRef?.height)!)
var bounds = CGRect(x: 0, y: 0, width: width, height: height)
var boundHeight:CGFloat
switch(orient)
{
case .up:
break
case .down:
break
case .left:
boundHeight = bounds.size.height
bounds.size.height = bounds.width
bounds.size.width = boundHeight
break
case .right:
boundHeight = bounds.size.height
bounds.size.height = bounds.size.width
bounds.size.width = boundHeight
break
default:
break
}
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytePerPixel:CGFloat = 4
let bytePerRow = Int(bytePerPixel * bounds.width)
let bitsPerComponent = 8
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
.union(.byteOrder32Big)
let context = CGContext.init(data: nil, width: Int(bounds.width), height: Int(bounds.height), bitsPerComponent: bitsPerComponent, bytesPerRow: bytePerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
if (orient == .right) {
context?.rotate(by: DEGREES_TO_RADIANS(270.0))
context?.translateBy(x: -width, y: 0)
}
else if (orient == .left) {
context?.rotate(by: DEGREES_TO_RADIANS(90.0))
context?.translateBy(x: 0, y: -height)
}
else if (orient == .down) {
context?.rotate(by: DEGREES_TO_RADIANS(180.0))
context?.translateBy(x: -width, y: -height)
}
else if (orient == .up) {
// NOTHING
}
context?.draw(imgRef!, in: CGRect(x: 0, y: 0, width: width, height: height))
let newImageRef = context?.makeImage()
let returnImage = UIImage.init(cgImage: newImageRef!)
return returnImage
}
'프로그래밍 > iOS' 카테고리의 다른 글
[ios] missing file (0) | 2015.04.09 |
---|---|
[ios] launch Image (0) | 2015.04.09 |
[iOS] static library에서 함수를 못읽어 올때 (0) | 2015.04.09 |
[ios] message sent to deallocated instance (0) | 2015.04.09 |
[ios] 갤러리에서 사진 가져오기 (0) | 2015.04.09 |