Convert content of UIScrollView in UIImage - objective-c

In my application, users can add UIImageView as subview of my UIScrollView. When the user taps "Save" button, I want to save all the content of my UIScrollView as an UIImage into the photo library.
I've been looking on Google but there is not much on the subject.
Edit: the below code does the job
CGRect rect = self.mainView.frame;
[self.mainView setFrame:CGRectMake(0, 0, 824, self.mainView.contentSize.height)];
UIGraphicsBeginImageContext(self.mainView.bounds.size);
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image , nil, nil, nil);
[self.mainView setFrame:rect];

Seems like you need to take screen shot of screen, you can try out this code on done action -
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:#"myImage.png" atomically:YES];

Related

How to take ScreenShot Of UIView Which is not in memory iOS

I want to convert UIView to UIImage, in which the view is in background and when I checked view hierarchy, it is there as white space, probably it wont get loaded..
What I wanted to do is load the view into memory and convert into image.
you just need to initialize your view with your required frame and then pass to below code as targetView. Initialize your view before you call screen capture method like below
UIView *targetView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)]; //Initilization
UIImage *capturedImage = [self captureScreen:targetView]; //Call method
- (UIImage *)captureScreen:(UIView*)targetView
{
UIView* captureView = targetView;
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0);
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect cropRect = CGRectMake(0 ,0 ,captureView.frame.size.width ,captureView.frame.size.height);
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.2f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return customScreenShot;
}
Use this:
- (UIImage*)snapShot:(UIView*)myView {
UIGraphicsBeginImageContext(myView.frame.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}

How to take my view as a picture osx

I want to take my NSView(With layers) as an image can I do so in OSX ?
in iOS I would do the following
-(UIImage*) makeImage//snapshot the view
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
I tried this solution from 1 of the SO answers
[[NSImage alloc] initWithData:[view dataWithPDFInsideRect:[view bounds]]];
but it didn't work because I have layers in my NSView.
I started from converting UI to NS
and now I have warnings on every line :)
-(NSImage*) makeImage :(RMBlurredView*)view
{
UIGraphicsBeginImageContext(view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
NSImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
I am not familiar with OSX can some 1 help me to convert this code to OSX ?
drawing to the image is tad different than in iOS:
you create a blank image, LOCK on to it, so all drawing goes to that graphics context
NSImage *image = [[NSImage alloc] initWithSize:self.view.bounds.size];
[image lockFocus];
you draw your layer same as on iOS
CGContextRef ctx = [NSGraphicsContext currentContext].graphicsPort;
[self.view.layer renderInContext:ctx];
unlock image
[image unlockFocus];
NOTE this is only if your view is layered. it doesn't work for non-layered views!
your view -as shown above- is layer backed so all's fine ;)

Saving a UIImage from a UIView not working properly

I'm attempting to save the contents of a UIView (and it's subviews) into a UIImage.
Here is the code I am using:
+(UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions([view bounds].size, YES, 0);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
It works great... the first time. However, if I then modify subviews of the view and call this method again, I get the old UIImage every time.
It's my understanding that UIGraphicsEndImageContext() does in fact pop the bitmap image off of the stack.
How can I make this imageWithView: method work, taking into account the current state of the view's subviews?
I hope this helps:
UIGraphicsBeginImageContext(self.view.frame.size);
[testView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Snap functionality in iOS

I have a parent UIView which contains 3 subviews, here i have one UIButton to take snap of the whole view. i have done this using
UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view.layer renderInContext:context];
here i need to capture only the 1st subview which name is subview1. how can i do this?
Capture SubView like this:
UIGraphicsBeginImageContext(subview1.size);
[subview1.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Dynamic UIView to UIImage is blank

I'm trying to create a UIView where I add 2 UIImageView inside and create an UIImage from this. Here is the method I use, but I get a blank UIImage.
Here is the code I use :
-(UIImage*)getFinalImage{
CGRect rect = CadreImage.frame;
UIView *dynaView = [[UIView alloc] initWithFrame:rect];
UIImageView *frontCadre = [[UIImageView alloc] initWithImage:TheImage];
[dynaView addSubview:frontCadre];
UIGraphicsBeginImageContextWithOptions(dynaView.bounds.size, dynaView.opaque, 0.0);
[dynaView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Any help would be appreciated.
Thanks.
Here is the code I use to grab a layer/screenshot (note this is for the whole screen and not a specific layer + handles retina displays):
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Can you also set a breakpoint and jump through the program to verify that TheImage is not nil?