【问题标题】:Cropping and scaling a UIImage properly正确裁剪和缩放 UIImage
【发布时间】:2013-08-02 09:23:09
【问题描述】:

我正在尝试将图像裁剪为正方形,然后将其缩放为 200x200 大小。这是我使用的代码,但它并不适合我。我得到的图像有时处于不同的方向,或者没有从中心裁剪,或者比应有的宽。

float scale = _avatar.image.scale;
UIImageOrientation orientation = _avatar.image.imageOrientation;

if(_avatar.image.size.width < _avatar.image.size.height){ // avatar is a UIImageView
    float startingY = (_avatar.image.size.height-_avatar.image.size.width)/2; // image is taller, determine the origin of the width-sized square, which will be the new image
    CGImageRef imageRef = CGImageCreateWithImageInRect([_avatar.image CGImage], CGRectMake(0, startingY, _avatar.image.size.width, _avatar.image.size.width));
    _avatar.image = [UIImage imageWithCGImage:imageRef scale:scale orientation:orientation];
    CGImageRelease(imageRef);
} else {
    float startingX = (_avatar.image.size.width-_avatar.image.size.height)/2; // image is wider, determine the origin of the height-sized square, which will be the new image
    CGImageRef imageRef = CGImageCreateWithImageInRect([_avatar.image CGImage], CGRectMake(startingX, 0, _avatar.image.size.height, _avatar.image.size.height));
    _avatar.image = [UIImage imageWithCGImage:imageRef scale:scale orientation:orientation];
    CGImageRelease(imageRef);
}

UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), YES, 0.0);
[_avatar.image drawInRect:CGRectMake(0, 0, 200, 200)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如何才能达到正确的结果?

【问题讨论】:

  • 有必要做吗?否则,您可以获取大小为 200*200 的图像视图并将内容模式设置为 aspectfill
  • 是的,我需要上传到网络服务器。

标签: ios resize uiimage core-graphics crop


【解决方案1】:

UIImage 已经为您完成了所有的坐标和方向技巧。所以你不应该使用 UIImage 对象的宽度和高度来计算正方形的坐标。您可以利用 UIKit 来裁剪图像。

CGFloat startingX = 0;
CGFloat startingY = 0;
CGFloat squareWidth;
if(_avatar.image.size.width < _avatar.image.size.height){ // avatar is a UIImageView  
    startingY = (_avatar.image.size.height-_avatar.image.size.width)/2; // image is taller, determine the origin of the width-sized square, which will be the new image  
    squareWidth = _avatar.image.size.width;
} else {
    startingX = (_avatar.image.size.width-_avatar.image.size.height)/2; // image is wider, determine the origin of the height-sized square, which will be the new image
    squareWidth = _avatar.image.size.height;
}  

UIGraphicsBeginImageContextWithOptions(CGSizeMake(squareWidth, squareWidth), YES, 0.0);
[_avatar.image drawAtPoint:CGPointMake(-startingX, -startingY)]; // Make an offset to draw part of the image
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();

UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), YES, 0.0);
[croppedImage  drawInRect:CGRectMake(0, 0, 200, 200)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【讨论】:

    【解决方案2】:

    我建议您将这个类别用于github 上发布的 UIImage。它们也是非常简单的类,您还可以使用它们来了解底层内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2022-01-11
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      相关资源
      最近更新 更多