- static inline double rad (int alpha)
- {
- return ((alpha * pi)/180);
- }
- static CGImageRef RotateImageWithFlip (CGImageRef source, float angle,
- BOOL flipX, BOOL flipY)
- {
- float fX = fabs ( cos ( rad ( angle ) ) );
- float fY = fabs ( sin ( rad ( angle ) ) );
- float dW = (float)CGImageGetWidth (source) * fX +
- (float)CGImageGetHeight (source) * fY;
- float dH = (float)CGImageGetWidth (source) * fY +
- (float)CGImageGetHeight (source) * fX;
- CGContextRef context = CGBitmapContextCreate (NULL,
- (size_t)dW, (size_t)dH,
- CGImageGetBitsPerComponent(source),
- 0,
- CGImageGetColorSpace(source),
- kCGImageAlphaPremultipliedLast);
- CGContextSetAllowsAntialiasing(context, NO);
- CGContextSetShouldAntialias(context, NO);
- CGContextSetInterpolationQuality (context, kCGInterpolationLow);
- CGAffineTransform transform =
- CGAffineTransformMakeTranslation(flipX?dW:0.0f,flipY?dH:0.0f);
- transform = CGAffineTransformScale (transform,flipX?-1.0:1.0f,flipY?-1.0: 1.0f);
- if (0.0f != angle)
- {
- CGAffineTransform rot = CGAffineTransformMakeTranslation (dW*0.5f,dH*0.5f);
- rot = CGAffineTransformRotate(rot, rad ( angle ));
- rot = CGAffineTransformTranslate (rot,-dH*0.5f,-dW*0.5f);
- transform = CGAffineTransformConcat(rot, transform);
- }
- CGContextConcatCTM(context, transform);
- CGContextDrawImage(context, CGRectMake (0, 0, CGImageGetWidth (source),
- CGImageGetHeight (source)), source);
- CGContextFlush(context);
- CGImageRef rotated = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- return rotated;
- }
- static CGImageRef RotateWithEXIF (CGImageRef source, int orientation)
- {
- switch (orientation)
- {
- case 2:
- return RotateImageWithFlip (source, 0, YES, NO);
- break;
- case 3:
- return RotateImageWithFlip (source, 0, YES, YES);
- break;
- case 4:
- return RotateImageWithFlip (source, 0, NO, YES);
- break;
- case 5:
- return RotateImageWithFlip (source, 90, NO, YES);
- break;
- case 6:
- return RotateImageWithFlip (source, -90, NO, NO);
- break;
- case 7:
- return RotateImageWithFlip (source, 90, YES, NO);
- break;
- case 8:
- return RotateImageWithFlip (source, -90, YES, YES);
- break;
- default:
- break;
- }
- return NULL;
- }
вторник, 11 сентября 2012 г.
Create CGImageRef with EXIF Orientation
Подписаться на:
Сообщения (Atom)