cho*_*ise 16 iphone cocoa-touch objective-c ios
所以目前我正在尝试裁剪和调整图片大小以使其适合特定尺寸而不会失去比例.
一个小图像,以显示我的意思:

我玩了一些vocaro的类别,但他们不使用png并且与GIF有问题.图像也不会被裁剪.
有没有人有一个建议如何这样做调整大小最好的方式或可能有一个链接到现有的库/类别/什么?
谢谢你的所有提示!
ps:ios是否实现了"选择一个摘录",以便我有正确的比例,只需要缩放它?!
这种方法可以满足您的需求,是易用的UIImage类别.我调整大小然后裁剪,你可以很容易地切换代码,如果你想要裁剪然后调整大小.函数中的边界检查纯粹是说明性的.您可能想要做一些不同的事情,例如将crop crop相对于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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12030 次 |
| 最近记录: |