Получаем метаданные изображения.
+ (NSDictionary*) exif : (NSString*) file
- {
- NSDictionary* dic = nil;
- NSURL* url = [ NSURL fileURLWithPath : file ];
- if ( url )
- {
- CGImageSourceRef source =
- CGImageSourceCreateWithURL ( (CFURLRef) url, NULL);
- if ( NULL == source )
- {
- #ifdef _DEBUG
- CGImageSourceStatus status = CGImageSourceGetStatus ( source );
- NSLog ( @"Error: file name : %@ - Status: %d", file, status );
- #endif
- }
- else
- {
- CFDictionaryRef metadataRef =
- CGImageSourceCopyPropertiesAtIndex ( source, 0, NULL );
- if ( metadataRef )
- {
- NSDictionary* immutableMetadata = (NSDictionary *)metadataRef;
- if ( immutableMetadata )
- {
- dic =
- [ NSDictionary dictionaryWithDictionary : (NSDictionary *)metadataRef ];
- }
- CFRelease ( metadataRef );
- }
- CFRelease(source);
- source = nil;
- }
- }
- return dic;
- }
Используем в коде - получение ориентации изображения из метаданных.
- NSDictionary* dic = [ ImageLoader exif : filename ];
- NSLog(@"exif : %@", dic);
- if ( dic )
- {
- NSString* s = [ dic valueForKey : @"Orientation" ];
- NSLog(@"Image : %@ - orentation : %d", filename, [ s intValue ] );
- }
LOG
2011-07-21 00:20:40.990 test.app[12204:8003] exif : {
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 7;
PixelHeight = 2592;
PixelWidth = 3872;
"{JFIF}" = {
DensityUnit = 1;
JFIFVersion = (
1,
1
);
XDensity = 72;
YDensity = 72;
};
"{TIFF}" = {
Orientation = 7;
};
}
2011-07-21 00:20:40.991 test.app[12204:8003] Image : /Users/alexey/Desktop/Works/EXIF Orientation Sample Images/7.jpg - orentation : 7
Как правильно преобразовать картинку с учетом ориентации
Как правильно преобразовать картинку с учетом ориентации
- 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;
- }
Комментариев нет:
Отправить комментарий