调整大小和裁剪图像居中 [英] resize and crop image centered

查看:172
本文介绍了调整大小和裁剪图像居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以目前我正在尝试裁剪和调整图片的大小,以适应它在一个特定的大小,而不会失去比例。



一个小图片显示我的意思:





我玩了 vocaro的类别,但他们不与png的工作,并有gifs的问题。



任何人都有建议如何调整大小的最佳方式,或者可能有一个链接到现有的库/类别/无论如何



ps:ios实现一个选择一个片段有正确的比例,只需要缩放它?

解决方案

这个方法会做你想要的,以方便使用。我去调整大小然后裁剪,你可以切换代码周围很容易,如果你想裁剪,然后调整大小。函数中的边界检查纯粹是说明性的。你可能想做一些不同的事情,例如,相对于outputImage维度来定位裁剪矩形,但是这应该能够让你足够接近你需要的任何其他更改。

  @implementation UIImage(resizeAndCropExample)

- (UIImage *)resizeToSize:(CGSize)newSize thenCropWithRect:(CGRect)cropRect {
CGContextRef context;
CGImageRef imageRef;
CGSize inputSize;
UIImage * outputImage = nil;
CGFloat scaleFactor,width;

//调整大小,保持宽高比:

inputSize = self.size;
scaleFactor = newSize.height / inputSize.height;
width = roundf(inputSize.width * scaleFactor);

if(width> newSize.width){
scaleFactor = newSize.width / inputSize.width;
newSize.height = roundf(inputSize.height * scaleFactor);
} else {
newSize.width = width;
}

UIGraphicsBeginImageContext(newSize);

context = UIGraphicsGetCurrentContext();

//添加2016.07.29,绘制前垂直翻转:
CGContextSaveGState(context);
CGContextTranslateCTM(context,0,newSize.height);
CGContextScaleCTM(context,1,-1);
CGContextDrawImage(context,CGRectMake(0,0,newSize.width,newSize.height,self.CGImage);

// //以其他方式绘制
// [ self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

CGContextRestoreGState(context);

outputImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

inputSize = newSize;

//限制crop rect到合法边界
if(cropRect.origin.x> = InputSize.width || cropRect.origin.y> = inputSize.height)return outputImage;
if(cropRect.origin.x + cropRect.size.width> = inputSize.width)cropRect.size.width = inputSize.width - cropRect.origin.x;
if(cropRect.origin.y + cropRect.size.height> = inputSize.height)cropRect.size.height = inputSize.height - cropRect.origin.y;

// crop
if((ImageRef = CGImageCreateWithImageInRect(outputImage.CGImage,cropRect))){
outputImage = [[[UIImage alloc] initWithCGImage:imageRef] autorelease];
CGImageRelease(imageRef);
}

return outputImage;
}

@end


so currently i'm trying to crop and resize a picture to fit it into a specific size without losing the ratio.

a small image to show what i mean:

i played a bit with vocaro's categories but they don't work with png's and have problems with gifs. also the image doesn't get cropped.

does anyone have a suggestion how to do this resizing the best way or probably have a link to an existing library/categorie/whatever?

thanks for all hints!

p.s.: does ios implement a "select a excerpt" so that i have the right ratio and only have to scale it?!

解决方案

This method will do what you want and is a category of UIImage for ease of use. I went with resize then crop, you could switch the code around easily enough if you want crop then resize. The bounds checking in the function is purely illustrative. You might want to do something different, for example center the crop rect relative to the outputImage dimensions but this ought to get you close enough to make whatever other changes you need.

@implementation UIImage( resizeAndCropExample )

- (UIImage *) resizeToSize:(CGSize) newSize thenCropWithRect:(CGRect) cropRect {
    CGContextRef                context;
    CGImageRef                  imageRef;
    CGSize                      inputSize;
    UIImage                     *outputImage = nil;
    CGFloat                     scaleFactor, width;

    // resize, maintaining aspect ratio:

    inputSize = self.size;
    scaleFactor = newSize.height / inputSize.height;
    width = roundf( inputSize.width * scaleFactor );

    if ( width > newSize.width ) {
        scaleFactor = newSize.width / inputSize.width;
        newSize.height = roundf( inputSize.height * scaleFactor );
    } else {
        newSize.width = width;
    }

    UIGraphicsBeginImageContext( newSize );

    context = UIGraphicsGetCurrentContext();

    // added 2016.07.29, flip image vertically before drawing:
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0, newSize.height);
    CGContextScaleCTM(context, 1, -1);
    CGContextDrawImage(context, CGRectMake(0, 0, newSize.width, newSize.height, self.CGImage);

//  // alternate way to draw
//  [self drawInRect: CGRectMake( 0, 0, newSize.width, newSize.height )];

    CGContextRestoreGState(context);

    outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    inputSize = newSize;

    // constrain crop rect to legitimate bounds
    if ( cropRect.origin.x >= inputSize.width || cropRect.origin.y >= inputSize.height ) return outputImage;
    if ( cropRect.origin.x + cropRect.size.width >= inputSize.width ) cropRect.size.width = inputSize.width - cropRect.origin.x;
    if ( cropRect.origin.y + cropRect.size.height >= inputSize.height ) cropRect.size.height = inputSize.height - cropRect.origin.y;

    // crop
    if ( ( imageRef = CGImageCreateWithImageInRect( outputImage.CGImage, cropRect ) ) ) {
        outputImage = [[[UIImage alloc] initWithCGImage: imageRef] autorelease];
        CGImageRelease( imageRef );
    }

    return outputImage;
}

@end

这篇关于调整大小和裁剪图像居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆