I am first to use UIGraphicsBeginImageContext.
I want to capture the visible part of the tableView to an UIImage and let it to the content of a new CALayer.
Here is my code:
UIGraphicsBeginImageContext(tableView.frame.size);
[tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect frame = [self.view.window convertRect:view.frame fromView:tableView.superview];
CALayer *imageLayer = [CALayer layer];
imageLayer.contents = (id)image.CGImage;
imageLayer.frame = frame;
However, if I scroll down the tableView, the top part of captured image will be white.
I use UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); to see the result.
After longtime finding the answer, I have to ask this question here, hope someone can help me!
Thanks!
Make a view called "viewHolder", set it's "clipsToBounds" to yes. Add tableView as a subview to viewHolder. Now use:
UIGraphicsBeginImageContext(viewHolder.frame.size);
[viewHolder.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Related
I have an app where I want to take a screenshot of a UIView when I am in another view. I have 4 views on one of my pages that can be toggled using a segmented control. But I want bmy view in the second segment to e displayed when I am in the first segment. Is there any way that I can do this?
This is what I have used so far:
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect;
rect=CGRectMake(0, 120, 320, 560);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
Screenshot = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return Screenshot;
This gets me the screenshot of the view I currently am in.
What I want to do is to take a screenshot of the second view while in the first view.
This is what I changed:
UIGraphicsBeginImageContext(View2.bounds.size);
For some reason this is not working? Can you tell me why and how to fix it?
You need to change [self.view.layer renderInContext:context]; to [self.view2.layer renderInContext:context];
UIGraphicsBeginImageContext(View2.bounds.size); Only specifies the bounds to render the view!
This is my Transparent image (Front image).
This is my selected image (Back Image).
I am giving gestures to the back image.
How can i save my image after using of pan, pinch, rotate gestures.
Present am able to combine both images but back image is saving as it is like (below image)
Using this code
CGSize newSize = CGSizeMake(640, 960);
UIGraphicsBeginImageContext( newSize);
[backImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frontImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(finalImage, nil, nil, nil);
I think i should handle this two lines but i don't know how to handle.
[backImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frontImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
Can any one help me will highly appreciated.
Thank you.
Please find the below code to get snapshot your imageview.
UIGraphicsBeginImageContextWithOptions(yourImageViewToSnapShot.bounds.size, NO, 0.0);
[yourImageViewToSnapShot.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I can take snapshots of the whole frame of the UIView with no problem.
Taking part of it cause to a squeezed image.
This is my UIView category method:
- (UIImage *)snapshot:(CGRect)frame
{
UIGraphicsBeginImageContextWithOptions(frame.size, YES, [[UIScreen mainScreen] scale]);
[self drawViewHierarchyInRect:frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
And this is the call to this method:
UIImage *img = [self.view snapshot:bottomRectToSnap];
When bottomRectToSnap is a partial frame of the view.
Note:The UIView contains UITableView if that matter.
The code you are applying is fine.
Need to add some logic, whatever part you need to take snap, It should be available on screen and hide rest of part while snapping.
Once snap shot done, remove hide code. So it will not effect to view interface too.
Use the following code.
- (void)captureView:(UIView*)view withFrame:(CGRect)frame{
UIGraphicsBeginImageContext(self.view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = frame;
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
CGImageRelease(imageRef);
}
You can add this code to take the snapshot of particular part..
CGRect cropRect=CGRectMake(0, 0, 100, 100);
CGImageRef imageRef = CGImageCreateWithImageInRect([_galleryimageview.image CGImage], cropRect);
UIImage *cropedImage=[UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
This code crop the part of particular image ...Hope this is helpfull for you..
I'm using this code to take a screenshot of my specific UIView which is inside in my main view.
UIGraphicsBeginImageContextWithOptions(_myView.frame.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *imageSave = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Main view size is (0,0,320,480) lets say, and myView size is (10,20,200,150)
Now i would like to capture/take a screenshot only of myView.
The above code doesn't capture exactly how i want, any suggestions?
Try this, it works for me.
- (UIImage *)snapshotOfView:(UIView *)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
I'm trying to get a UIView screen capture on iOS.
In my view I have two UIImageView with animations.
The code that I using it to capture the view is:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Read the UIImage object
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:didFinishSavingWithError:contextInfo:), nil);
The result is an image with the background and only one UIImageView. Both ImageViews are in moviming all the time. If I take several pictures, then the UIImageView is in the same position each time.
Try this one:
-(CGImageRef)imageCapture
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect= CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
return imageRef;
}
use the below line whenever you want to capture the screen
UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];