resize和裁剪图像的中心

所以目前我正在尝试裁剪和调整图片以适应特定尺寸而不会丢失比例。

一个小图像来展示我的意思:

替代文字

我玩了一些vocaro的类别,但他们不与PNG的工作,并与gifs的问题。 也不会裁剪图像。

有没有人有一个build议如何做到这一点调整最佳方式,或者可能有一个链接到现有的图书馆/ categorie /什么?

感谢所有的提示!

PS:是不是执行“select一个摘录”,以便我有正确的比例,只有规模呢?!

这个方法将做你想要的,并且是一个UIImage类的易用性。 我去resize然后裁剪,如果你想裁剪,然后resize,你可以很容易地切换代码。 函数中的边界检查纯粹是说明性的。 你可能想要做一些不同的事情,例如,将裁剪matrix相对于outputImage维度居中,但是这应该让你足够接近,做出你需要的任何其他更改。

@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 

我在我的一个应用程序中遇到了同样的问题,并开发了这段代码:

 + (UIImage*)resizeImage:(UIImage*)image toFitInSize:(CGSize)toSize { UIImage *result = image; CGSize sourceSize = image.size; CGSize targetSize = toSize; BOOL needsRedraw = NO; // Check if width of source image is greater than width of target image // Calculate the percentage of change in width required and update it in toSize accordingly. if (sourceSize.width > toSize.width) { CGFloat ratioChange = (sourceSize.width - toSize.width) * 100 / sourceSize.width; toSize.height = sourceSize.height - (sourceSize.height * ratioChange / 100); needsRedraw = YES; } // Now we need to make sure that if we chnage the height of image in same proportion // Calculate the percentage of change in width required and update it in target size variable. // Also we need to again change the height of the target image in the same proportion which we /// have calculated for the change. if (toSize.height < targetSize.height) { CGFloat ratioChange = (targetSize.height - toSize.height) * 100 / targetSize.height; toSize.height = targetSize.height; toSize.width = toSize.width + (toSize.width * ratioChange / 100); needsRedraw = YES; } // To redraw the image if (needsRedraw) { UIGraphicsBeginImageContext(toSize); [image drawInRect:CGRectMake(0.0, 0.0, toSize.width, toSize.height)]; result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } // Return the result return result; } 

你可以根据你的需要修改它。