How to capture the screen shot for transition - iphone

I want to capture the screenshot for the transition between images.
- (UIImage *)captureView:(UIView *)view
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[view.layer.presentationLayer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I call this method during a UIImageView playing transition of images, however, I just got the still image in the UIImageView but cannot capture the transition between images.
Anyone can help me out?

In the simulator you can hit shift 3 times to slow animation down, you might be able to catch the transition then.

Related

Resolution of capture screen image is not retina

I am using the following code for capture a view on the screen but it is not as sharp as on the screen. the imageView size is 200x200point but the scale is 2.0 (with retina screen). The saved img size is 200x200 px. how could i make the img as sharp as the original one? Any help will be appreciated!
- (UIImage*)captureView:(UIView *)theView {
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Try like that:
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
You are using the old way of creating the image context. Use this instead:
UIGraphicsBeginImageContextWithOptions(rect.size, YES, [UIScreen mainScreen].scale);
The older UIGraphicsBeginImageContext function always assumes a scale of 1.0.
Replace 'YES' with 'NO' if you need the alpha channel.

How to take a screenshot programmatically in iOS?

I have a UIView that uses both UIKit control and OpenGL. I'd like to get a screenshot of that view programatically.
If I use UIGraphicsGetImageFromCurrentImageContext(), the OpenGL content is blank;
If I use the glReadPixels(...) method, the UIKit content is blank;
I'm confused as how to take a complete screenshot.
Thank you!
use following code to take screen shot
-(UIImage *) screenshot
{
CGRect rect;
rect=CGRectMake(0, 0, 320, 480);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
-(UIImage *) screenshot
{
CGImageRef UIGetScreenImage(void);
CGImageRef screen = UIGetScreenImage();
UIImage* screenImage = [UIImage imageWithCGImage:screen];
CGImageRelease(screen); // you need to call this.
return screenImage;
}
Well, there are few ways of capturing the iPhone screen programmatically
Using UIKIT http://developer.apple.com/library/ios/#qa/qa1703/_index.html
Using AVFoundation framework http://developer.apple.com/library/ios/#qa/qa1702/_index.html
Using OpenGL ES
http://developer.apple.com/library/ios/#qa/qa1704/_index.html
Starting from iOS 7 you can also use Why does my programmatically created screenshot look so bad on iOS 7?
Using UIWindow
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.window.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Using UIView
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Out of these 6 options, I found the first option very convenient for copy-pasting and for applying level of compression as 1st method gives the image with true pixel data. I also like option 4, as the API comes with SDK.
You get result Uiimage without lose quality of image
- (UIImage *)captureView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UImage *screenShot = [UIImage imageWithData:data];
You know where the OpenGL content is located in your view. So it should be easy to copy the result of glReadPixels into the snapshot of the UIKit.
maybe.. can you use something from here
- https://stackoverflow.com/questions/12413460/cocos2d-2-0-screenshots-on-ios-6
and here...
Why is glReadPixels() failing in this code in iOS 6.0?
Here is how you can take a simple screenshot programmatically of a UIImage. If you want it for any other object substitute the UIImage with it.
In the .h file add NSData *Data; then in your .m file add this to your code
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *attachimage = [[UIImage alloc]initWithData:Data];
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
attachimage = viImage;
UIImageWriteToSavedPhotosAlbum(viImage, nil, nil, nil);

How to take the screenshot when playing the MPMoviePlayer?

How can I take screenshot when playing the video in MPMoviePlayer? I am using the below code for take screenshot but the player screen is black color.
1)
- (UIImage*)captureScreenshot:(UIView *)view {
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
return viewImage;
}
2) CGImageRef originalImage = UIGetScreenImage();
This is apple banned.
Please give any other way to get screenshot from the movie player. Please help me.
Somewhat hacktastic, but you could take a screenshot, grab a thumbnail from the movie player (e.g. as suggested here) and overlay the second on the first.

how to get snap shots of touch part on screen by programming?

I am implementing iphone application in which I want to implement below features.
When user touch on iphone screen then user snap shot will generate of touch area of the screen and save to photo library.
I have done googling but dont get successed.
Please help me for this query.
Thanks in advance
If you are looking for sample code to control the camera. Here is a bare bones Camera application that takes picture and saves it to library
Check out this method, pass the view touched to this method, will send u the image & save it to library.
- (UIImage*) giveScreentshotsOfView:(UIView*) view
{
if (view == nil)
return nil;
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *) captureView:(UIView *)view
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//save the image to photo album
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil , nil);
}

iPhone: UIImage obtained after rendering a view is solid black

I'm using this method to render a UIView into a UIImage:
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
The resulting image is of the right size (as shown by the UIImage's size property), but its contents is solid black colour. The view passed there definitely contains some graphics, but it's not rendered. Any idea why?
This is the code I use to capture images of a UIView:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
Your code looks fine to me. You can try swapping mine out to see if it helps, but I'd cite it to be a separate issue from the code you posted.