iOS: recortando una imagen fija tomada de una cámara UIImagePickerController con superposición
Soy nuevo en iOS y he estado golpeando la cabeza contra la pared por esto durante la última semana buscando tutoriales en línea como:Tratar con imágenes Exif, Cambiar el tamaño de las imágenes, y muchas más preguntas al azar aquí en StackOverflow. A partir de estos, pensé que>=iOS 4.0
, todas las imágenes tomadas de la cámara contienenEXIF
información de rotación basada en
Lo que no funciona: Después de probar diferentes técnicas de recorte de imagen, todo lo que termino es recortar la imagen en algunas esquinas aleatorias y también, la imagen resultante parece estar ampliada :( Cuando uso unpng
imagen de internet (que no contiene datos EXIF), el recorte está funcionando. Por esquinas aleatorias, quiero decir, la imagen termina siendo recortada en la esquina superior derecha / superior izquierda y ampliada.
Lo que estoy tratando de lograr:
Estoy tratando de recortar una imagen100 px
desde arriba y100 px
desde la parte inferior. Esencialmente, estoy usando dos tiras superpuestas: 1 en la parte superior y 1 en la parte inferior con laCGRect(0.0, 0.0, SCREEN_WIDTH, 100.0)
[un100.0 px
tira alta en la parte superior] y otraCGRect(0.0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100.0)
[otro100 px
tira alta en la parte inferior]. Necesito obtener la imagen entre estas dos tiras: supongo que la altura de la imagen es:SCREEN_HEIGHT - 200.0
.
Muestra de UIImagePickerController para cámara con superposición:
//SCREEN_HEIGHT = 480 and SCREEN_WIDTH = 320
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
{
NSLog(@"Camera not available");
return;
}
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
// Hide the controls
imagePicker.showsCameraControls = NO;
imagePicker.navigationBarHidden = YES;
// Make camera view full screen
imagePicker.wantsFullScreenLayout = YES;
//imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
//Overlay
//OverlayView is a plain UIView with the CGRects mentioned in the question.
OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
[overlay initOverlay:self];
imagePicker.cameraOverlayView = overlay;
[self presentModalViewController:imagePicker animated:YES];
Code para rotar la imagen en base a EXIFimageOrientation
propiedad y recortarla
- (UIImage *) cropImage: (UIImage *) originalImage
{
CGRect cropRect = CGRectMake(0, 100.0, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
CGRect transformedRect = [self TransformCGRectForUIImageOrientation:cropRect :originalImage.imageOrientation :originalImage.size];
CGImageRef resultImageRef = CGImageCreateWithImageInRect(originalImage.CGImage, transformedRect);
UIImage *newImage = [[[UIImage alloc] initWithCGImage:resultImageRef scale:1.0 orientation:originalImage.imageOrientation] autorelease];
return newImage;
}
- (CGRect) TransformCGRectForUIImageOrientation: (CGRect) source: (UIImageOrientation) orientation: (CGSize) imageSize {
switch (orientation) {
case UIImageOrientationLeft: { // EXIF #8
CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(
imageSize.height, 0.0);
CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,
M_PI_2);
return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationDown: { // EXIF #3
CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(
imageSize.width, imageSize.height);
CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,
M_PI);
return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationRight: { // EXIF #6
CGAffineTransform txTranslate = CGAffineTransformMakeTranslation(
0.0, imageSize.width);
CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate,
M_PI + M_PI_2);
return CGRectApplyAffineTransform(source, txCompound);
}
case UIImageOrientationUp: // EXIF #1 - do nothing
default: // EXIF 2,4,5,7 - ignore
return source;
}
LoscropImage
l método @ parece funcionar para las imágenes descargadas de Internet (que no contienen ninguna información de orientación). Me estoy quedando sin opciones. ¿Podría alguien ayudarme
¡Gracias por leer