iPhone:如何裁剪图像在ios5 [英] iPhone:How to crop image in ios5

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

问题描述

在我的应用程序中,要求是从照片库或相机抓取图像,如果用户想裁剪图像,那么他应该能够做到,但我不知道如何裁剪从相机抓取的图像或照片库。

In my application the requirement is to grab image from the photo library or camera and if user wants to crop the image then he should be able to do it but I do not have any idea how to crop the image fetched from camera or photo library.

如果您有任何想法,然后分享...

if you have any idea then share it...

推荐答案

您可以使用 UIGraphicsGetImageFromCurrentImageContext

这个想法是使用CoreGraphics绘制您的图像到裁剪的图形上下文,然后将当前上下文导出到图像。

The idea is to draw your image to a cropped graphic context using CoreGraphics and then exporting the current context to an image.


UIGraphicsGetImageFromCurrentImageContext基于当前基于位图的图形上下文的内容返回基于
的图像。

UIGraphicsGetImageFromCurrentImageContext Returns an image based on the contents of the current bitmap-based graphics context.

返回一个包含当前
位图图形上下文内容的图像对象。

Return A image object containing the contents of the current bitmap graphics context.

/ p>

Try something like that :

- (UIImage*)imageCrop:(UIImage *)imageToCrop toRect:(CGRect)rect
{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    CGContextClipToRect( currentContext, clippedRect);

    CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y, imageToCrop.size.width, imageToCrop.size.height);
    CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return cropped;
}

希望这有帮助,
Vincent

hope this helps, Vincent

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

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