【问题标题】:How to Crop a UiImage to specific Size?如何将 UiImage 裁剪为特定大小?
【发布时间】:2017-05-09 17:23:40
【问题描述】:

您好,我正在使用 UIimage,我需要将图像裁剪为特定尺寸,假设我的特定尺寸宽度等于设备宽度,高度固定为 200。 我在 Google 中尝试了很多示例,但没有一个不适合我,我尝试了以下示例

-(UIImage *)resizeImage:(UIImage *)image
{
    float h=200;
    float w=400;


    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float maxHeight = h;
    float maxWidth = w;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = maxWidth/maxHeight;
    float compressionQuality = 0.8;//50 percent compression

    if (actualHeight > maxHeight || actualWidth > maxWidth)
    {
        if(imgRatio < maxRatio)
        {
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio)
        {
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else
        {
            actualHeight = maxHeight;
            actualWidth = maxWidth;
        }
    }
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    //UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
    UIGraphicsEndImageContext();
    return [UIImage imageWithData:imageData];
}

下面是我的实物图

裁剪后我得到如下所示

我的所有示例都在拉伸图像但我想在没有拉伸的情况下缩放图像。我在哪里失踪?请帮助我。

【问题讨论】:

  • picker.allowsEditing = YES;当您捕获图像并当时选择图像时,设置一个属性并尝试放置
  • 还有一个问题,只是将 imageview 属性设置为适合而不是 scall 来填充
  • 我认为,这是您的尺寸问题,在调整图像大小后打印您的宽度和高度。我想你会得到一个矩形框架,这就是为什么图像是这样的
  • 打印实际宽度和实际高度
  • 只有当我从肖像模式横向模式正常工作时拍摄图像时才会出现问题

标签: ios iphone uiimage crop scaling


【解决方案1】:

您使用的代码将调整您的图像大小,我们将其用于图像压缩或图像大小调整。 如果要裁剪图像,有两种方法。 1)允许用户在选择图像时进行编辑。 您可以通过将 allowEditing 属性添加到选择器来实现这一点,

pickerView.allowEditing = true

这会给你一个矩形来裁剪图像,你可以从选择器视图的 didFinish 方法中检索编辑的图像

2)以编程方式

  • 要以编程方式裁剪图像,您需要传递帧进行裁剪。

这是我正在使用的功能

func croppImage(originalImage:UIImage, toRect rect:CGRect) -> UIImage{

var imageRef:CGImageRef = CGImageCreateWithImageInRect(originalImage.CGImage, rect)
var croppedimage:UIImage = UIImage(CGImage:imageRef)
return croppedimage
}

上面的代码会返回给你裁剪的图像。

【讨论】:

  • 我尝试了以上两种方法,但裁剪了图像拉伸
  • 如果您的 UIImageView 与裁剪后的图像具有不同的高宽比,图像将被拉伸。在内容模式下使用 aspectFit。
  • 我接受,但我需要在 uiimageview 中填充我的图像。如果我使用 aspectFit 它不会填充正确吗?那我该怎么办?
  • 是的,它不会填满您的图像视图。没有这样的方法可以在横向图像视图填充中显示您的纵向图像而不是拉伸。或者至少我还不知道。
【解决方案2】:

这些会将您的图像裁剪成您需要的大小。

- (CGImageRef)newTransformedImage:(CGAffineTransform)transform
                      sourceImage:(CGImageRef)sourceImage
                       sourceSize:(CGSize)sourceSize
                sourceOrientation:(UIImageOrientation)sourceOrientation
                      outputWidth:(CGFloat)outputWidth
                         cropSize:(CGSize)cropSize
                    imageViewSize:(CGSize)imageViewSize
{
    CGImageRef source = [self newScaledImage:sourceImage
                             withOrientation:sourceOrientation
                                      toSize:sourceSize
                                 withQuality:kCGInterpolationNone];

    CGFloat aspect = cropSize.height/cropSize.width;
    CGSize outputSize = CGSizeMake(outputWidth, outputWidth*aspect);

    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 outputSize.width,
                                                 outputSize.height,
                                                 CGImageGetBitsPerComponent(source),
                                                 0,
                                                 CGImageGetColorSpace(source),
                                                 CGImageGetBitmapInfo(source));
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, outputSize.width, outputSize.height));

    CGAffineTransform uiCoords = CGAffineTransformMakeScale(outputSize.width / cropSize.width,
                                                            outputSize.height / cropSize.height);
    uiCoords = CGAffineTransformTranslate(uiCoords, cropSize.width/2.0, cropSize.height / 2.0);
    uiCoords = CGAffineTransformScale(uiCoords, 1.0, -1.0);
    CGContextConcatCTM(context, uiCoords);

    CGContextConcatCTM(context, transform);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(-imageViewSize.width/2.0,
                                           -imageViewSize.height/2.0,
                                           imageViewSize.width,
                                           imageViewSize.height)
                       , source);

    CGImageRef resultRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGImageRelease(source);
    return resultRef;
}

- (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientation)orientation toSize:(CGSize)size withQuality:(CGInterpolationQuality)quality
{
    CGSize srcSize = size;
    CGFloat rotation = 0.0;

    switch(orientation)
    {
        case UIImageOrientationUp: {
            rotation = 0;
        } break;
        case UIImageOrientationDown: {
            rotation = M_PI;
        } break;
        case UIImageOrientationLeft:{
            rotation = M_PI_2;
            srcSize = CGSizeMake(size.height, size.width);
        } break;
        case UIImageOrientationRight: {
            rotation = -M_PI_2;
            srcSize = CGSizeMake(size.height, size.width);
        } break;
        default:
            break;
    }

    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 size.width,
                                                 size.height,
                                                 8,  //CGImageGetBitsPerComponent(source),
                                                 0,
                                                 CGImageGetColorSpace(source),
                                                 (CGBitmapInfo)kCGImageAlphaNoneSkipFirst  //CGImageGetBitmapInfo(source)
                                                 );

    CGContextSetInterpolationQuality(context, quality);
    CGContextTranslateCTM(context,  size.width/2,  size.height/2);
    CGContextRotateCTM(context,rotation);

    CGContextDrawImage(context, CGRectMake(-srcSize.width/2 ,
                                           -srcSize.height/2,
                                           srcSize.width,
                                           srcSize.height),
                       source);

    CGImageRef resultRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    return resultRef;
}

///要裁剪的位置。

CGImageRef imageRef = [self newTransformedImage:transform
                                    sourceImage:self.image.CGImage
                                     sourceSize:self.image.size
                              sourceOrientation:self.image.imageOrientation
                                    outputWidth:self.image.size.width
                                       cropSize:self.photoView.cropView.frame.size
                                  imageViewSize:self.photoView.photoContentView.bounds.size];

UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

谢谢。享受编码:)

【讨论】:

  • 感谢重播如何分配给我的图像。
  • 我更新了上面的帖子。检查。如果你得到你想要的,请投票给我的答案。
猜你喜欢
  • 2012-01-03
  • 1970-01-01
  • 2017-02-06
  • 2012-03-12
  • 2010-10-10
  • 2011-09-25
  • 1970-01-01
相关资源
最近更新 更多