如何在目标c中裁剪图像? [英] How to crop the image in objective c?

查看:247
本文介绍了如何在目标c中裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以更改编辑屏幕中默认显示的裁剪框大小。我试过下面的代码:

The user can change the cropbox size which is shows default in edit screen. I tried with below code :

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef); 
    return cropped;
}

但它裁剪固定区域。如何裁剪用户选择的区域?

But it cropped fixed area. How to crop area which is selected by user ?

推荐答案

获取裁剪图像:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake("AS YOu Need"); //set your rect size.
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

使用以下代码调用 croppIngimageByImageName:toRect:返回<$的方法c $ c> UIImage (具有特定尺寸的图像

Use following code for call croppIngimageByImageName:toRect: method that return UIImage (with specific size of image)

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

这篇关于如何在目标c中裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆