Reputation: 5160
I have this function that taking a screenshot of the a small UIView
that I have on my app, my problem is that I want to add to this image a background that located in my images folder. The background that I want to add is not showing on the current view, I just want to paste it behind the screen shot image programmatically before I'm saving the image. Is it possible?
(I don't want to create a new view with the background on it and than to add the screenshot image and than to take the screen shot again. it's too much)
Lets say that this is the Image that has been taken:
______
/ \
/ \
\ /
\______/
And this is the background image with the image together:
_________________
| |
| ______ |
| / \ |
| / \ |
| \ / |
| \______/ |
| |
|_________________|
This my code:
- (void)takeScreenShotAndSaveIt:(CGPoint)center
{
// IMPORTANT: using weak link on UIKit
if(UIGraphicsBeginImageContextWithOptions != NULL)
{
UIGraphicsBeginImageContextWithOptions(smallview.frame.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(smallview.frame.size);
}
[smallview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save image to iPhone documents directory
NSData * imgData = [[NSData alloc] initWithData:UIImagePNGRepresentation(viewImage)];
NSString * filename = [NSString stringWithFormat:@"MeasureScreenShot.png"];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
[imgData writeToFile:path atomically:YES];
}
Upvotes: 0
Views: 92
Reputation: 69047
If I understand correctly what you are trying to do, try this:
- (void)takeScreenShotAndSaveIt:(CGPoint)center
{
...
[backgroundImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[smallview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
...
where backgroundImageView
is an UIImageView containing your background image. You simply render the two views in the same context. If you prefer handling your background as an image instead of a view, you can use:
[bckImage drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
Upvotes: 1