capture time remove the tool bar - iphone

-(IBAction)printDisplay:(id)sender{
CGRect rect = CGRectMake(0.0,44.0,self.view.frame.size.width,self.view.frame.size.height);
NSValue *rectObj = [NSValue valueWithCGRect:rect];
CGRect rectRestored = [rectObj CGRectValue];
UIGraphicsBeginImageContext(CGSizeMake(rectRestored.size.width, rectRestored.size.height-44));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
This is code for capture the image from the current screen.
Problem is that I don't want to get the UIToolBar for the capture into the screen for that what to do. My application screenshot looks like this :
and after I take a screenshot of the image :
In short I want the remove the UIToolBar when the snap of the image.
Thanx in advance for spend your valuable time.
#samuel

if the image frame is starting from below the Toolbar, you can just set the rect starting from (0,44). If the image frame is starting from beneath the toolbar from (0.0) just set the toolbarObject.hidden = YES; before taking the screenshot through graphicsContext and reset it back to toolbarObject.hidden = NO; afterwards.

Keep your images and extra stuff(what ever contain you want to capture) in different view and use that view for capture.
In your code lets say Second View named as secondView So code will be :
-(IBAction)printDisplay:(id)sender{
CGRect rect = CGRectMake(0.0,44.0,self.secondView.frame.size.width,self.secondView.frame.size.height);
NSValue *rectObj = [NSValue valueWithCGRect:rect];
CGRect rectRestored = [rectObj CGRectValue];
UIGraphicsBeginImageContext(CGSizeMake(rectRestored.size.width, rectRestored.size.height-44));
[self.secondView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

Modify the code so that the line
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
is
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];

Related

Maintain image resolution in screen grab

In my app, the user is able to put stickers on top of a photo. When they go to save their creation, I do a screen grab and store it in a UIImage:
UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
(where self.mainView has a subview UIImageView which holds the photo, and another subview UIView which holds the stickers).
I am wondering, is it possible to do a screen shot in this manner, and maintain the resolution of the aforementioned photo?
The following will 'flatten' two UIImages into one while maintaining the resolution of the original image(s):
CGSize photoSize = photoImage.size;
UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0);
CGRect photoRect = CGRectMake(0, 0, photoSize.width, photoSize.height);
// Add the original photo into the context
[photoImage drawInRect:photoRect];
// Add the sticker image with its upper left corner set to where the user placed it
[stickerImage drawAtPoint:stickerView.frame.origin];
// Get the resulting 'flattened' image
UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The above assumes photoImage and stickerImage are both instances of UIImage and stickerView is a UIView with containing the stickerImage and thus will be able to use the stickerView frame to determine its origin.
If you have multiple stickers, just iterate through the collection.
If you are looking to save an image of your current view then this might help you.
UIGraphicsBeginImageContext(self.scrollView.contentSize);
[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(finalImage.CGImage,
CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y,
scrollView.frame.size.width, scrollView.frame.size.height));
UIImage *screenImage = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
CGImageRelease(imageRef);

Take a snapshot of a PKAddPassesViewController's view

I'm trying to take a snapshot of the view displayed when the user is prompted to add a pass.
The goal is being able to generate an image from a given pkpass
I was hoping something like this could work:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"columbus" ofType:#"pkpass"];
PKPass *pass = [[PKPass alloc] initWithData:[NSData dataWithContentsOfFile:filePath] error:nil];
PKAddPassesViewController *pkAddPassesViewController = [[PKAddPassesViewController alloc] initWithPass:pass];
[self presentViewController:pkAddPassesViewController animated:YES completion:^(){
UIImage *image = [pkAddPassesViewController.view captureView];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
}];
With captureView being:
- (UIImage *)captureView
{
CGRect screenRect = self.bounds;
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
CGContextFillRect(ctx, screenRect);
[self.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Guess what? I was wrong:
I tried:
Using the completion callback and the animationFinished callback
Setting the hidden property
Using a timer to make sure the pass was visible on screen when the screenshot was taken
Taking a snapshot of the parent view
I ran out of ideas, so any suggestion is appreciated
Thanks in advance
Oh, I've uploaded a minimal project here:
https://github.com/framp/demo-capturing-passbook
EDIT:
I'm able to take a snapshot of other views and I can load other UIImage just fine with the code above.
The UIImage which is returned is a blank image
Try this, this might work for you.
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData=UIImagePNGRepresentation(reportImage);
The capture image code is good. Try with : [self.view.superview addSubview:imageView]; in the completion block instead of [self.view addSubview:imageView];
You can try this code if that work for you. Give the Coordinates which you want to capture and thats it. It will save your file in image album. It may give a warning but it works.
UIGraphicsBeginImageContext(CGSizeMake(320, 380));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
I think pkAddPassesViewController is not fully loaded when you taken snapshot. So my suggestion is to take screenshot after some delay.
I hope it will work.

Xcode 4.3 How do I save a region of my app screen?

I am creating a native app on Xcode 4.3 and deploying to target iOS 5. The app is a basically a greeting card creator. I am having trouble figuring out how to save a portion of the screen from within the app.
What I want to do is this:
I am offering the user a button, that says "e-mail". when they click the button, the app should 'save' their card as an image and then 'paste' that into an email body.
The reason this is different than other answers on this website is that the area I want to save is made up of 4 'elements'. There is a background graphic that is the tilted card background, then there is a text field where users can type a message and then next to that is a picture area where they can choose their own picture to put on the card.
Here is a photo of what I am talking about:
http://marklopezdesigns.com/mydownloadz!/screenshotCard3.png
How do I save a 'composite' high res of these?
And then how do I get that into an email body message?
The reason i am asking how to 'save' it is because I want to be able to offer users another button that says "save to camera roll" and "send as message". I figure if I can understand how to save this high-res to a variable, then I should be off and running.
Thanks in advance for the help.
========
Here's the solution below
========
...so after a bit of fiddling. Finally got what I wanted. Here's the codebase I have in my method that fires upon touch of the "Save to Album" button:
- (IBAction)savePhoto{
CGRect rect;
rect = CGRectMake(11,50 ,305, 262);
UIView *cardViewer = [[UIView alloc] initWithFrame:rect];
UIGraphicsBeginImageContext(cardViewer.bounds.size);
//make view background transparent
cardViewer.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
cardViewer.opaque = NO;
//stuff items into a subview for capturing
[self.view addSubview:cardViewer];
[cardViewer addSubview:self.tiltedCard];
[cardViewer addSubview:self.bigCardView];
[cardViewer addSubview:self.cardWords];
[cardViewer addSubview:self.photoView];
[cardViewer.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
//put everything back where it belongs
[self.view addSubview:self.tiltedCard];
[self.view addSubview:self.bigCardView];
[self.view addSubview:self.cardWords];
[self.view addSubview:self.photoView];
[cardViewer removeFromSuperview];
}
To capture just an area of the screen, specify the bounds using CGRectMake.
CGRect rect = CGRectMake(50, 50, 100, 100);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
In the example above we are capturing a 100px by 100px region beginning at x:50px and y:50px.
maybe get the picture of the layer by rendering the layer context and grabbing the image, I use this for displaying fancy animations
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
To capture just an area of the screen, use the UIView or SubView and just specify the bounds using CGRectMake.
CGRect rect = CGRectMake(10, 10, 200, 200);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.YourView.layer renderInContext:context]; // Make sure here you are using the greetingcardView rather than self.view
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Note: self.view will capture the whole screen so simply render the view wherever.

Capturing screenshot in iphone?

I want to take screenshot programatically. So that i write code for it like,
CGContextRef context = [self createBitmapContextOfSize:self.frame.size];
//not sure why this is necessary...image renders upside-down and mirrored
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height);
CGContextConcatCTM(context, flipVertical);
[self.layer renderInContext:context];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);
self.currentScreen = background;
Here all code is in custom UIView of UIViewController. Now when i play UIImageView png sequence animation on UIView, then i didn't get updated changes in UIImageView which is subview of custom UIView, why? I got only result is UIImageView with first UIImage.
Basically i need to create an video of my game play like talking tom application.
Take the screenshot of your ImageView with something like this:
[[[yourImageView layer] presentationLayer] renderInContext:context];
Use NSTimer for calling that getScreenshot function.
Hope this helps
you should probably take a look at AVFoundation and specifically AVAssetWriter as a way of creating videos of your screen content.
Following code is use for the capture the image into the iPhone for that following code to be use.
CGRect rect = CGRectMake(0.0, 0.0,self.view.frame.size.width,self.view.frame.size.height-44);
NSValue *rectObj = [NSValue valueWithCGRect:rect];
CGRect rectRestored = [rectObj CGRectValue];
UIGraphicsBeginImageContext(CGSizeMake(rectRestored.size.width, rectRestored.size.height-44));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
Also store the image into the gallery.

Saving two Overlapping UIImage

I am trying to build a photo frame application on iphone. I made the frame it is transparent in png formate, then by choosing photos and was placed behind the frame layer in the interface builder.
In interface builder they are placed well and fit well. Now my problem is how can i save them into one picture.
Here is the code i have, but the saving part keep crashing.
-(IBAction) saveImage:(id)sender{
imagefront .backgroundColor = [UIColor clearColor]; //This sets your backgroung to transparent.
imagefront.opaque = NO;
[imageView bringSubviewToFront:imagefront];
UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(overlappedImage, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
Imagefront is the photoframe while imageView is the photo.
Thank you.
Your current approach is incorrect. You will need to do this to get the image.
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsGetCurrentContext();
This is assuming that imageView has imageFront as its subview as suggested by the code you've posted.