I have a UIimageview that i have a transparent rect that i can resize by drag the problem is i couldn't save the image (with the recant above)
i tried
-(UIImage *)imageWithViewOld:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
but it only show my UIImageview without my custom UIview
i even tried adding
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
[self.myRect.layer renderInContext:UIGraphicsGetCurrentContext()];
but in the generated image i can see the rect but above my image not inside
It sounds like you have an image view and you want to drag the transparent view over the image view and saved a cropped version of the image view. The issue with how it sounds like your code is set up, is that both of these views are siblings. The easiest solution you can do is add the transparent view as a subview to the image view and then use -drawViewHierarchyInRect:afterScreenUpdates:.
-(UIImage *)imageWithViewOld:(UIView *)view croppedTo:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view drawViewHierarchyInRect:rect afterScreenUpdates:NO];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Related
I want to capture UIView and save as image. Here is my code
+ (UIImage *)captureView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
// CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
// CGContextFillRect(context, view.bounds);
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
It works fine. But I get one issue: I can not change background for that image. It always gets black background.
So, how to change background?
Thanks.
The reason why you cannot change the background for a UIImage is because UIImage does not possess a background attribute. To add a background for which you can change color, you're going to have to create a custom UIView on your own for which the UIImage can be placed on top of. So for example, let's say you wanted to create a function which returns an image with a background of your choice:
- (UIImage* )setBackgroundImageByColor:(UIColor *)backgroundColor withFrame:(CGRect )rect{
// tcv - temporary colored view
UIView *tcv = [[UIView alloc] initWithFrame:rect];
[tcv setBackgroundColor:backgroundColor];
// set up a graphics context of button's size
CGRectGSize gcSize = tcv.frame.size;
UIGraphicsBeginImageContext(gcSize);
// add tcv's layer to context
[tcv.layer renderInContext:UIGraphicsGetCurrentContext()];
// create background image now
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
// [tcv release];
}
// [view drawViewHierarchyInRect:view.frame afterScreenUpdates:NO]; // comment this line
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; // use this line
it gives the actual color of the view's as it is.. if your view's background color is clear color or black color, the image background color will be black color
I am using UIActivityViewController for email, facebook, twitter and sms sharing..
I have successfully shared image as well text.
But, now i want to share UIView. I wanted to know is it possible or not?
If yes please let me know how.
Thanks in advance.
You can have a UIImage from a UIView,
Here is what you should be using if you are only supporting iOS7+:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
Otherwise use this:
#import <QuartzCore/QuartzCore.h>
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
You can take a screenshot of a UIView object. See please following topic:
How to capture UIView to UIImage without loss of quality on retina display
I have successfully shared image as well text.
After getting UIImage from UIView, you can share appropriate image.
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 create kaleidoscope effect from uiview .it use a loft of sublayer and sublayer mask .
then i use below code for render uiimage from uiview.
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
render image as below:
The problem is the render image doesn't render mask effect.
I would like to render a UIView into a UIImage. However, I would like to render it much larger than it is on screen, without pixilation.
At present I am using...
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
[self.pageViewController.view.layer renderInContext:context];
renderedViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I have tried...
self.pageViewController.view.transform = CGAffineTransformMakeScale(4.0f, 4.0f);
This changes the size of the view but the original size is rendered before the view is scaled.
I have also tried...
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 4.0, 4.0);
but this just scales up the low resolution image giving a poor quality result.
Setting the...
UIView contentScaleFactor
hasn't helped me render the UIView any larger either.
Try This you will get 2x size image of view if device supports retina
- (UIImage *)imageOfView:(UIView *)view
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(view.bounds.size);
}
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}