iPhone: Get camera preview - iphone

I'd like to get the image that is being displayed on the UIImagePickerController when user uses the camera. And when I get I want to process the image and display instead of regular camera view.
But the problem is when I want to get the camera view, the image is just a black rectangle.
Here's my code:
UIView *cameraView = [[[[[[imagePicker.view subviews] objectAtIndex:0]
subviews] objectAtIndex: 0]
subviews] objectAtIndex: 0];
UIGraphicsBeginImageContext( CGSizeMake(320, 427) );
[cameraView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageToDisplay.image = [PixelProcessing processImage: viewImage]; //In this case the image is black
//imageToDisplay.image = viewImage; //In this case the image is black too
//imageToDisplay.image = [UIImage imageNamed: #"icon.png"]; //In this case image is being displayed properly
What am I doing wrong?
Thanks.

This one is also working quite good. Use it when the camera preview is open:
UIImage *viewImage = [[(id)objc_getClass("PLCameraController")
performSelector:#selector(sharedInstance)]
performSelector:#selector(_createPreviewImage)];
But as far as I found out it brings the same results than the following solution which takes a 'screenshot' of the current screen:
extern CGImageRef UIGetScreenImage();
CGImageRef cgoriginal = UIGetScreenImage();
CGImageRef cgimg = CGImageCreateWithImageInRect(cgoriginal, rect);
UIImage *viewImage = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgoriginal);
CGImageRelease(cgimg);
A problem I didn't still find a fix for is, how can one get the camera image very fast without any overlays?

The unofficial call is:
UIGetScreenImage()
which you declare above the #implementation as:
extern CGImageRef UIGetScreenImage();
There may be a documented way to do this in 3.1, but I'm not sure. If not, please please file a Radar with Apple asking them to make some kind of screen grab access public!!!
That uses your same AppleID you log in to the iPhone development portal with.
Update: This call is not yet documented, but Apple explicitly has given the OK to use it with App Store apps.

at least for now, there's no way to do this. (certainly no official documented way, and as far as I know nobody's figured out an unofficial way either.)
the camera preview data is being drawn by the OS in some way that bypasses the normal graphics methods.

Related

Save UIView with CALayer.mask as UIImage in iOS 6

I have the following code:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(mainView.bounds.size, NO, [UIScreen mainScreen].scale);
}
else {
UIGraphicsBeginImageContext(mainView.bounds.size);
}
[mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
In mainView, there is a masked subview that does not appear in saveImage when using this method. However, I understand there used to be a UIGetScreenImage method pre iOS 4 that did capture such activity. My question is, what is the best way to capture CALayer activities in iOS 6? Is UIGetScreenImage still private?
I think there was a similar question about a week ago: Mask does not work when capturing a uiview to a uiimage
On iOS6 there is a problem capturing a UIView with the mask applied (btw, in iOS 7 it has been fixed): you capture the image but the mask is not applied.
I posted a lengthy solution which involved applying the mask manually to the captured image. It's not very difficult and I also made a demo project of this. You can download it here:
https://bitbucket.org/reydan/so_imagemask
If I did not understand your problem correctly, please tell me so I can remove this answer.
try getting the presentation layer instead, as it will contain the layer's state.
[mainView.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/presentationLayer

Generate a pdf from opengles context in iphone/ipad

Hi am a noob at PDF generation and currently am displaying a 3d bar charts in my application in glview which is in side a uiview. Now i want to generate a PDF document from the uiview which contains a text view and a glview. Am able to generate a PDF from text but where as GL View am unable to proceed.even i tried for a possibility over the net but unable to get any info regarding this.Can someone help me by providing the solution whether this can be possible or not.Thanks in Advance
You can ask any UIView to give you an image (which might be a start) via something like:
- (UIImage *) imageRepresentation {
// render myself (or more correctly, my layer) to an image
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldSmoothFonts(context, false);
CGContextSetShouldAntialias(context, false);
[self drawLayer:self.layer inContext:context];
[self.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
After that, you might be able to get a PDF representation of it, or investigate calls like CGPDFDocumentCreateWithURL to construct one yourself from the image.
You can use CoreGraphic's PDF-APIs to creating and drawing to a PDF.
See Apple's guide for more details.

Custom image mask in iOS

I have a issue with masking images. I do game "puzzle" and have to make custom images. I found and tried 2 way of custom cropping:
Using CALayer.mask property.
Using UIImage.mask property.
In first option i create my custom path, then assign it to CAShapeLayer.path property, then assign CAShapeLayer to CALayer.mask property. At the end i have custom cropped image.
In second option i use firstly use CGImageMaskCreate() method (i use previously created black mask images of puzzle), then CGContextClipToMask().
In either options i have problem with performance (mostly when i crop image into 16 puzzles and drag in over the screen).
Is there any other approaches to crop image in custom way.
(I don't know how to solve performance problem).
Thanks in advance.
There are lots of UIImage-categories out there you can use for this. Give me a moment and I'll post some links here:
Cropping an UIImage (not really a category though, but it'll fit)
UIImage: Resize, then Crop
https://sites.google.com/a/injoit.com/knowledge-base/for-developers/graphics/uiimage-routines-scaling-cropping-rotating-etc
http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/
http://maybelost.com/2010/11/cropping-a-uiimage-on-iphone/
Try this:
-(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
...
UIImage *temp_image = [self imageByCropping:original_image toRect:clipping_rectangle];
Maybe you should consider about drawing the image in a new image with an alpha background an overdrawing the current background. I mean: All pixel which are inside the jigsaw piece: normal colour, all pixels outside the jigsaw piece = transparent. And then try to blend it to the new background or overdrawing it.
Just my 2 cents. :)

iPhone Screenshots

I've been using UIGetScreenImage() to get a screenshot of a UIImagePickerController. Basically I use the camera overlay and then when I take the screenshot, I have the image that the camera preview had been showing and my overlay on there too, which is exactly what I need.
Now UIGetScreenImage() has been banned, I've not been able to find a way to do this. It just shows black for the camera.
Edit: all of my other views are showing absolutely fine, just not the actual camera preview. Any ideas??!?!?
Here's the code I am using at the moment.
UIGraphicsBeginImageContext(picker.view.bounds.size);
[picker.view.layer renderInContext:UIGraphicsGetCurrentContext()];
CGContextDrawImage(context, bounds, camView.CGImage);
UIImage* screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);
UIGraphicsEndImageContext();
Any ideas how I can get the overlay + the camera image?
Thanks!
This is slightly different than yours.
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum (sShot, nil, nil, nil);
Does that work?
If not, you might want to try asking over at iphonedevsdk.com since they specialize in all things iPhone.
Have them take a picture. Then overlay your image on top of it.
Create a UIImage from two other UIImages on the iPhone

Get what PLCameraView is showing on 3.0

with fw 2.2 i was able to get a screenshot using the private method _createCGImageRefRepresentationInFrame of UIWindow. In 3.0 doesn't exist anymore.
I used that method with the PLCameraView over a window to take a small video just by get as much screenshot as possible. Now i tried with the CALayer of the PLPreviewView and -renderInContext: method, but it always render the view as it has the iris closed.
How can i take a screenshot of what the cameraView is showing?
Thanks
Marco
Try for following code snippet to get
CGImageRef imageRef = [[UIApplication sharedApplication] _createDefaultImageSnapshot];
UIImage* img = [UIImage imageWithCGImage:imageRef];
//Now you can save it the way you want
//May as following
//oops yes this image is just the screenshot so better take care of unwanted image side
//So cut crap out from the image captured
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);