【问题标题】:How to crop the image in objective c?如何在目标 c 中裁剪图像?
【发布时间】:2013-08-13 09:41:48
【问题描述】:

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

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

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

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

【问题讨论】:

  • 尝试将不同的 CGRect 传递给您的给定函数,它将起作用。我不认为你的代码有任何问题。你的语言基础有问题。我建议,请阅读一些关于 Objective-C 的书籍或苹果文档。

标签: iphone ios objective-c


【解决方案1】:

获取裁剪图像:

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

使用以下代码调用 croppIngimageByImageName:toRect: 方法,返回 UIImage具有特定大小的图像

- (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;
    }

【讨论】:

    【解决方案2】:
        CGRect clippedRect  = CGRectMake(0 ,0,180 ,180);
        CGImageRef imageRef = CGImageCreateWithImageInRect(imgVw1.image.CGImage, clippedRect);
        UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        imgVw1Cliped.image=newImage;
    
        NSLog(@"%d",imgVw1Cliped.image.imageOrientation);
    

    【讨论】:

    • @user2677875 当您对答案感到满意时,请为这个答案投票。bcz 您的投票将对 Mohit 有所帮助。这个答案在我的项目中有效。
    • @Mohit 此代码似乎不再有效。给 ios8 一个错误。
    猜你喜欢
    • 2011-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2015-02-02
    相关资源
    最近更新 更多