我真的需要模拟NSImage的drawInRect行为:fromRect:operation:使用UIImage的分数,我想知道什么是最好的方法或(更好),如果有人已经做了这并愿意分享代码。我试过这样做使用CGImageCreateWithImageInRect(见代码在最后),但我没有得到预期的结果。请注意,代码实际上是在Mac上测试这个(我不能运行在iPhone截至目前整个事情),所以我可能有一些问题与NSImage中仿真NSImage drawInRect:fromRect:operation:使用UIImage的分数
- (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);
}