views:

199

answers:

0

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