Pregunta

Estoy escribiendo código para utilizar UIImagePickerController. Corey publicados con anterioridad algún buen ejemplo de código de SO relacionada con el recorte y escalado. Sin embargo, no tiene implementaciones de cropImage: a: andScaleTo:. Ni straightenAndScaleImage ()

He aquí cómo se usan:

newImage =  [self cropImage:originalImage to:croppingRect andScaleTo:scaledImageSize];
...
UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);

Desde que estoy seguro de que alguien debe estar usando algo muy similar al código de ejemplo de Corey, es probable que haya una implementación existente de estas dos funciones. ¿Alguien quiere compartir?

¿Fue útil?

Solución

Si marca la entrada que está conectado, verá un enlace a los foros dev manzana donde me dieron algo de este código, aquí están los métodos que está preguntando por. Nota: Puede que haya hecho algunos cambios relativos a los tipos de datos, pero no puedo recordar bien. Debe ser trivial para ajustar si es necesario.

- (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -size.height);
CGContextDrawImage(context, myRect, subImage);
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(subImage);
return croppedImage;

}

UIImage *straightenAndScaleImage(UIImage *image, int maxDimension) {

CGImageRef img = [image CGImage];
CGFloat width = CGImageGetWidth(img);
CGFloat height = CGImageGetHeight(img);
CGRect bounds = CGRectMake(0, 0, width, height);
CGSize size = bounds.size;
if (width > maxDimension || height > maxDimension) {
    CGFloat ratio = width/height;
    if (ratio > 1.0f) {
        size.width = maxDimension;
        size.height = size.width / ratio;
    }
    else {
        size.height = maxDimension;
        size.width = size.height * ratio;
    }
} 

CGFloat scale = size.width/width;
CGAffineTransform transform = orientationTransformForImage(image, &size);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip 
UIImageOrientation orientation = [image imageOrientation];
if (orientation == UIImageOrientationRight || orientation == UIImageOrientationLeft) {

    CGContextScaleCTM(context, -scale, scale);
    CGContextTranslateCTM(context, -height, 0);
}else {
    CGContextScaleCTM(context, scale, -scale);
    CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, bounds, img);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

}

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top