Pergunta

Eu estou escrevendo código para usar UIImagePickerController. Corey postado anteriormente um código de exemplo agradável em SO relacionadas com recorte e dimensionamento. No entanto, ele não tem implementações de cropImage: para: andScaleTo:. Nem straightenAndScaleImage ()

Veja como eles são usados:

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

Desde que eu tenho certeza que alguém deve estar usando algo muito semelhante ao código de exemplo de Corey, há provavelmente uma implementação existente destas duas funções. Será que alguém gostaria de compartilhar?

Foi útil?

Solução

Se você verificar o post ligado a você, você verá um link para os fóruns dev maçã onde eu tenho alguns deste código, aqui estão os métodos que você está perguntando sobre. Nota: Talvez eu tenha feito algumas alterações relativas aos tipos de dados, mas eu não consigo me lembrar. Deve ser trivial para você ajustar, se necessário.

- (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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top