iphone - 执行转换后 "invalid frame"到底意味着什么?

标签 iphone objective-c ios cocoa-touch core-graphics

我有一个简单的应用程序,允许用户使用平移和捏合手势识别器在屏幕上拖动/缩放图像。 Apple API 的常识是,在执行转换后(正如我在缩放方法中所做的那样),框架将变得“无效”。由于我不断使用 ImageView 的框架来计算比例大小和平移距离,即使在执行变换并且一切继续正常工作之后,我想知道框架变得无效怎么办?

我问这个,因为现在我正在尝试裁剪已移动和缩放的图像,并且我使用其帧值来确定裁剪区域,并且我得到了有趣的结果。我想知道无效的框架是否可能是问题所在,或者是否可能是其他问题。这是我的代码供您引用:

CGRect croppedRect = CGRectMake(fabsf(self.oldImageView.frame.origin.x), fabsf(self.oldImageView.frame.origin.y), 316, 316);
CGImageRef croppedImageRef = CGImageCreateWithImageInRect(self.oldImageView.image.CGImage, croppedRect); 
UIImage *croppedImage = [UIImage imageWithCGImage: croppedImageRef];
CGImageRelease(croppedImageRef);
UIImageView *newImageView = [[UIImageView alloc] initWithImage: croppedImage];

最佳答案

无效的框架 = 有趣的结果。

我认为无效意味着它不能保证有错误的值,但有可能。您已经幸运地使用了该框架。

如果您有转换后的 View ,并且需要框架,则可以使用以下代码:

CGRect frame = [[view superview] convertRect:view.bounds fromView:view];

只要不旋转。如果旋转,“框架”就不能表示为 CGRect,它只能表示与轴对齐的矩形。如果该信息对您的程序有用,您可以使用 convertPoint 转换边界的每个角。

或者设置框架(如果不旋转)

view.bounds = [view convertRect:newFrame fromView:[view superview]];

关于iphone - 执行转换后 "invalid frame"到底意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8069868/

相关文章:

iphone - 如何让付费版和免费版的 iOS 应用共享 FB App ID?

ios - 是否可以将Unity与UIKit混合使用?

iphone - 如何为 iphone 购物车应用程序改进这些 UML 图? (sqlite、parsxml 委托(delegate)、httprequest)

iphone - Apple 是否批准使用 setKeepAliveTimeout :handler methode for updating GPS location to server?

javascript - 专门针对 CSS 规则而不是 chrome 在 iPhone 上针对 Safari 浏览器?

ios - 解析 filterResults findObjects 与 findObjectsInBackgroundWithBlock

ios 将 View 保留在内存中

ios - 点击手势改变颜色

ios - 使用 Trigger.io 检测 iOS 状态栏上的点击

ios - 更改 UIAlertView 中 UITextField 的字体大小