How to create image from screenshot and send to message interface - objective-c

I am very new to Objective-C. I am attempting to create an image from a screen shot and send it to the message interface to be sent as a message. I have looked at numerous Stackoverflow questions and one suggests use of snapshotViewAfterScreenUpdates which is new in iOS 7. It returns a UIView pointer but I do not understand how to make an image from that and then to the message interface to be sent as a message. Any help would be greatly appreciated.

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *newPNG=UIImagePNGRepresentation(img); // or you can use JPG or PDF
To share the image, then you can use UIActivityViewController:
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:#"I would like to share it.",newPNG, nil] applicationActivities:nil];
activityVC.excludedActivityTypes = #[ UIActivityTypeMessage ,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:YES completion:nil];

This is you you get UIImage from screen:
UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Related

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();

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?

iOS UIAppearance Error

I'm building a file management app, and I occasionally get the following error while calling a UIImagePickerController or a MPMediaPickerController:
*** -[_UIImageViewPretiledImageCacheKey hash]: message sent to deallocated instance 0x140dc0
I recently gave my app a custom theme using iOS 5's UIAppearance API and thats when I started getting this error. By guessing and checking, I found the problematic lines of my code that cause this error:
UIImage *backButtonImage = [[UIImage imageNamed:#"backButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 16, 12, 8)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
UIImage *barButtonImage = [[UIImage imageNamed:#"barButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(14, 12, 14, 12)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I have no idea how this code triggers the above error. Can you please explain to me the source of this error and provide a solution to fix it.
Thanks in advance for your help,
Guvvy
After some more thorough testing, I have reached the conclusion that this problem is limited to retina devices. The issue turned out to be in the #2x images. They had a odd numbered resolution (eg. 59px by 60px). All I did was recreate the image and changed the resolution to 60px by 60px and I never experienced the problem again.
I was kind of surprised by the solution as I saw no correlation between the error message and the line of code, but in the end, it was the images that caused this problem.
I had a similar problem, but my crash was caused by a resizable image in a UIImageView.
My resizable image has edge insets of top=30px, left=20px, bottom=1px, right=10px. The image is 43x45, so its resizable area is 13x14. I'm using iOS6, so I was able to workaround the problem by specifying UIImageResizingModeStretch as the resizingMode for -[UIImage resizableImageWithCapInsets:resizingMode:].
Works:
UIImage *image = [UIImage imageNamed:name];
UIImage *resizableImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:UIImageResizingModeStretch];
Crashes with EXC_BAD_ACCESS or [_UIImageViewPretiledImageCacheKey hash]: message sent to deallocated instance 0xb14deb0 with NSZombieEnabled:
UIImage *image = [UIImage imageNamed:name];
UIImage *resizableImage = [image resizableImageWithCapInsets:edgeInsets];
I got the same for the following code
UIImage* image = [UIImage stretchableImageNamed:#"imageName"];
self.backgroundView = [[UIImageView alloc] initWithImage:image];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:image];
where self is UITableViewCell and stretchableImageNamed: is simply
+(UIImage*)stretchableImageNamed:(NSString*)name
{
UIImage *img = [UIImage imageNamed:name];
CGSize sz = img.size;
int left = (sz.width - 1.) / 2.;
int top = (sz.height - 1.) / 2.;
return [img resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, top, left)];
}
this helped:
self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageNamed:#"imageName"]];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage stretchableImageNamed:#"imageName"]];

Convert content of UIScrollView in UIImage

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];