iPhone - 如何制作可调整大小的矩形来裁剪图像?

发布于 2024-08-16 09:16:02 字数 432 浏览 8 评论 0原文

我在制作可调整大小的矩形来裁剪图像时遇到问题。我正在尝试实现类似下图的效果:

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

好吧,唯一的问题是我不知道从哪里开始。我需要一些关于如何实现这种裁剪效果的建议。我应该阅读哪些文档?核心显卡还是 Quartz 2d?两个都?自从 iPhone 发布以来,我一直在为它编写代码,但我从未真正使用过核心显卡等。任何帮助或建议将不胜感激。随着我的进展,我将把我的代码放在这里,以展示当我实现它时它是如何完成的。 :-) 另外,这个矩形框可以在 UIImageView 中跨屏幕移动,这使它更有趣。感谢您的帮助,我期待实现这一目标。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒眉千度 2024-08-23 09:16:02

为我的问题添加书签,该问题完全相同。

我已经在裁剪视图中移动并使其正确显示图像的部分,但处理滚动视图缩放是一项正在进行的工作。目前我有一个 imageview 和普通 UIView 分层在 UIScrollview 中,然后在滚动视图之外和更高级别是我的自定义裁剪 UIView。我实现了 Touchs 方法来处理拖动它的问题,还实现了 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 引用了其他视图中的底层图像。

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

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

我实现了一些 UIScrollViewDelegate 方法并传递滚动活动,以便我可以保持裁剪视图同步。缩放也被传递了,但正如前面提到的,还没有按预期工作。

另一个注意事项:我认为没有必要每次都浪费时间生成带有裁剪区域的新 UIImage,因为同样应该可以使用

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

bookmark my question which is exactly the same.

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)];
}

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" 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

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.

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);
尐偏执 2024-08-23 09:16:02

Quartz 是 Core Graphics 的“营销术语”,这就是您应该开始的地方。您将需要使用 UIView(在 -drawRect: 中进行完全自定义绘图),捕获并跟踪其触摸,然后绘制结果。您可以使用 UIGraphicsGetCurrentContext() 获取当前的图形上下文。这应该足以让您开始了!

Quartz is the 'marketing term' for Core Graphics, and that's where you should start. You'll want to use a UIView (full custom drawing in -drawRect:), trap and track its touches, and then draw the result. You can get the current Graphics Context using UIGraphicsGetCurrentContext(). That should be enough to get you started!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文