【问题标题】:cropping an UIImage to a new aspect ratio将 UIImage 裁剪为新的纵横比
【发布时间】:2011-10-06 06:07:16
【问题描述】:

我想裁剪 UIImage 以获得新的纵横比...

bounds 是一个 CGRect,带有(0,0, newwidth, newhigh)...

- (UIImage *)croppedImage:(UIImage *)myImage :(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect(myImage.CGImage, bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGSize asd =  croppedImage.size;
    return croppedImage;
}

随叫随到:

[workImage croppedImage: workImage: CGRectMake(0, 0, newWidth, newHeigh)];

之后“工作图像”的大小与之前相同......

可能出了什么问题?

问候

【问题讨论】:

    标签: iphone xcode ipad uiimage crop


    【解决方案1】:

    好吧,您并没有更改当前图像,因为这似乎是UIImage 上的一种类别方法。您正在创建一个新图像并返回它。所以这会起作用,

    workImage = [workImage croppedImage: workImage: CGRectMake(0, 0, newWidth, newHeigh)];
    

    但是我认为这个方法最好这样命名,(假设它是UIImage上的一个类别方法)

    - (UIImage *)croppedImageWithRect:(CGRect)bounds {
        CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, bounds);
        UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        CGSize asd =  croppedImage.size;
        return croppedImage;
    }
    

    这样你会这样称呼它,

    workImage = [workImage croppedImageWithRect:CGRectMake(0, 0, newWidth, newHeigh)];
    

    附带说明,不要使用croppedImage:: 之类的方法。最好将所有参数命名为 croppedImage:rect:

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多