使用 UIImage 模拟 NSImage drawInRect:fromRect:operation:fraction
我真的需要使用 UIImage 来模拟 NSImage 的 drawInRect:fromRect:operation:fraction 的行为,我想知道什么是最好的方法或(更好),如果有人已经这样做并且愿意分享代码。
我尝试使用 CGImageCreateWithImageInRect 执行此操作(请参阅最后的代码),但我没有得到预期的结果。请注意,代码实际上是在 mac 上测试的(我现在无法在 iPhone 上运行整个代码),所以我可能在从 NSImage 获取 CGImage 时遇到一些问题
- (void)drawInRect:(CGRect)rect fromRect:(CGRect)fromRect alpha:(float)alpha {
CGContextRef context;
CGImageRef originalImageRef;
CGImageRef subimageRef;
#if ERMac
context=[[NSGraphicsContext currentContext] graphicsPort];
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)[self TIFFRepresentation], NULL);
originalImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);
#else
context=UIGraphicsGetCurrentContext();
originalImageRef=CGImageRetain([self CGImage]);
#endif
subimageRef = CGImageCreateWithImageInRect (originalImageRef, fromRect);
CGContextDrawImage(context, rect, subimageRef);
if (subimageRef)
CGImageRelease(subimageRef);
if (originalImageRef)
CGImageRelease(originalImageRef);
}
I really need to emulate the behavior of NSImage's drawInRect:fromRect:operation:fraction using UIImage and I'm wondering what's the best approach or (better), if somebody already did this and willing to share the code.
I tried doing this using CGImageCreateWithImageInRect (see code at the end) but I'm not getting the expected results. Note that the code is actually testing this on the mac (I can't run the whole thing on the iPhone as of now) so I may have some issue with getting the CGImage from NSImage
- (void)drawInRect:(CGRect)rect fromRect:(CGRect)fromRect alpha:(float)alpha {
CGContextRef context;
CGImageRef originalImageRef;
CGImageRef subimageRef;
#if ERMac
context=[[NSGraphicsContext currentContext] graphicsPort];
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)[self TIFFRepresentation], NULL);
originalImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);
#else
context=UIGraphicsGetCurrentContext();
originalImageRef=CGImageRetain([self CGImage]);
#endif
subimageRef = CGImageCreateWithImageInRect (originalImageRef, fromRect);
CGContextDrawImage(context, rect, subimageRef);
if (subimageRef)
CGImageRelease(subimageRef);
if (originalImageRef)
CGImageRelease(originalImageRef);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我就是这样做的...
This is how I do it...
UIImage有drawInRect:blendMode:alpha:。提取 UIImage 子矩形的一个简单技巧是绘制到目标上下文中,该目标上下文是要提取的矩形的大小,向上和向左调整绘制图像的原点,以便右侧部分落入目标中语境。
CGImage 也是一个很好的方法,但是您可能会遇到在 iPhone 上翻转坐标系的问题。一个简单的解决方案是将 CGImage 包装到 UIImage 中并绘制它,因为 UIImage 知道向上绘制的方向。
UIImage has drawInRect:blendMode:alpha:. A simple trick for extracting a subrectangle of a UIImage is to draw into a target context which is the size of the rectangle you want to extract, adjusting the origin of the drawn image up and to the left so that the right portion falls into the target context.
CGImage is also a good way to do this but you may encounter problems with the coordinate system being flipped on the iPhone. An easy solution is to wrap the CGImage into a UIImage and draw that, since UIImage knows which way up to draw.
NSImage类别方法示例和我移植到iOS:
iOS代码:
NSImage category method example and my port to iOS:
iOS code: