1

I am trying to access pixel data of screen. Very strangely the method worked for ipad 3, but failed on ipod touch 4. First, I used some lines from FastBlurredNotificationCenter to grab a UIImage from the screen:

IOSurfaceRef surface = [UIWindow createScreenIOSurface];
UIImage *imageui = [[UIImage alloc] _initWithIOSurface:surface scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

This surely works on both devices.

Then, I tried to get a CGImage from UIImage:

CGImageRef imagecg = imageui.CGImage

This code doesn't work on ipod(cgimage size={0,0}). So I assume the UIImage may be created from CIImage and added a line to test:

NSLog(@"debug-ciimage:%@-cgimage:%@",imageui.CIImage,imageui.CGImage);

Both results are "null". I don't know how else to access pixel data of UIImage execpt from its CGImage or PNG representation. Please help...

list of methods tried: 1. [UIImage CGImage]; 2. [UIImage CIImage]; 3. [UIImage drawInRect:imageRect]; (with context given by UIGraphicsBeginImageContextWithOptions(contextsize, NO, 0.0f);)

add: Solved the problem in a way. But the image type of UIImage created from IOSurface is still unknown... The solution is to avoid that UIImage that I can't deal with... From the iphonedevwiki, there is an alternative for -(id)initWithIOSurface:(IOSurfaceRef)ioSurface;

that is

CGImageRef UICreateCGImageFromIOSurface(IOSurfaceRef ioSurface);

It works on both devices.

The conclusion is that initWithIOSurface is suitable for you if you want to use it on UIView, otherwise use UICreateCGImageFromIOSurface so that you can easily convert it to what you like.

Thanks to JustSid for helping my 1st question on stackoverflow.

wesley6j
  • 1,393
  • 9
  • 17
  • What happens when you try to draw the image into a graphics context? – JustSid May 30 '13 at 14:33
  • good idea. Maybe this will works. I'll try. i did a bit search. but it seems that i need the cgimage to draw on cgcontext? CGContextDrawImage(context, drawRect, newImage.CGImage); – wesley6j May 30 '13 at 14:43
  • `UIImage` defines its own drawing methods which you can use (though I'm not sure if they aren't just wrappers over `CGContextDrawImage()` or if they implement some more logic) – JustSid May 30 '13 at 14:49
  • How do I find these methods or could you kindly provide one? Haven't find anything yet from google or stackoverflow. – wesley6j May 30 '13 at 14:57
  • It's right there in the [documentation](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/instm/UIImage/drawInRect:) – JustSid May 30 '13 at 15:01
  • I tried the following, still not working( blank image on ipod and success on ipad). CGSize contextsize = CGSizeMake(imageui.size.width, imageui.size.height); UIGraphicsBeginImageContextWithOptions(contextsize, NO, 0.0f); CGRect imageRect = CGRectMake(0.0, 0.0, imageui.size.width, imageui.size.height); [imageui drawInRect:imageRect]; UIImage *imageui1=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); – wesley6j May 30 '13 at 15:51
  • I guess the method drawInRect failed to draw if the uiimage is not from cgimage (ciimage). – wesley6j May 30 '13 at 15:54

0 Answers0