iPhone-如何制作可调整大小的矩形以裁剪图像? [英] iPhone - How do you make a resizable rectangle for cropping images?

查看:161
本文介绍了iPhone-如何制作可调整大小的矩形以裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作一个可调整大小的矩形以裁剪图像时遇到麻烦.我正在尝试实现以下图片:

I'm having a trouble making a re-sizable rectangle for cropping my images. I'm trying to achieve something like this picture:

http://img192.imageshack.us/img192/8930/customcropbox.jpg

嗯,唯一的问题是我不知道从哪里真正开始.我需要一些建议,以了解如何实现这种裁剪效果.我应该阅读哪些文档?核心图形还是Quartz 2d?两个都?自发布以来,我一直在为iPhone编写代码,但是我从未真正使用过核心图形等.任何帮助或建议都将不胜感激.我将在这里逐步介绍我的代码,以显示实现它时的完成方式. :-)此外,此矩形框可以在UIImageView中的整个屏幕上移动,这只会使它更加有趣.感谢您的帮助,我期待实现这一目标.

Well, the only problem is I have no clue where to actually start. I need some advice to how I can achieve this effect of cropping. What documentation should I read up on? Core Graphics or Quartz 2d? Both? I've been coding for the iPhone since it's release date but I've never actually used core graphics and etc. Any help or advice would be much appreciated. I'll throw my code up here as I progress to show how it's done when I achieve it. :-) Also, this rectangular box is moveable across the screen in a UIImageView which just makes it more interesting. Thanks for the help and I look forward to achieving this goal.

推荐答案

将我的问题添加为书签完全一样.

bookmark my question which is exactly the same.

我尽力在裁剪视图周围移动并使其正确显示图像的一部分,但是处理滚动视图缩放是一项正在进行的工作.目前,我在UIScrollview中有一个imageview和一个普通的UIView,然后在scrollview之外并在更高级别上是我的自定义裁剪UIView.我实现了touches方法来处理它的拖动,还实现了drawRect:

I got as far as moving around my cropping view and having it show the portion of the image correctly, but dealing with the scrollview zooming is a work in progress. presently I have an imageview and plain UIView layered in the UIScrollview, then outside of the scrollview and at a higher level is my custom cropping UIView. I implement the touches methods to deal with dragging it around, and also drawRect:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    if( self.image == nil )
        return;

    CGPoint offset = scrollView.contentOffset;
    clipRect = CGRectOffset(self.frame, offset.x, offset.y);

    UIImage *croppedImage = [image croppedImage:clipRect];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [croppedImage drawAtPoint:CGPointMake(0, 0)];
}

注意:我的裁剪视图不是UIImageView的后代,而是UIView,它引用了另一个视图中的基础图像.

Note: my cropping view is NOT a descendant of UIImageView, it's just UIView with a reference to the underlying image from the other view.

"croppedImage"是UIImage上的一个类别,非常简单:

"croppedImage" is a category on UIImage and pretty simple:

@implementation UIImage (Resize)
- (UIImage *)croppedImage:(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}
...
@end

我已经实现了一些UIScrollViewDelegate方法并通过滚动活动,因此我可以使裁剪视图保持同步.缩放也是通过的,但是如上所述,还不能按需工作.

I've implemented a few of the UIScrollViewDelegate methods and pass through scrolling activity so I can keep my cropping view in sync. Zooming is passed along too, but as mentioned, not working as desired yet.

另一个注意事项:我认为不必浪费时间每次都用裁剪区域生成新的UIImage,因为

Another Note: I don't think it's necessary to waste the time generating a new UIImage with the cropped area every time as the same should be possible with

CGImageRef imageRef = CGImageCreateWithImageInRect([yourImage CGImage], bounds);
and 
CGContextDrawImage(ctx, clipRect, imageRef);

这篇关于iPhone-如何制作可调整大小的矩形以裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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