Email Sharing using UIActivityViewController - ios

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.

Related

Capturing UIImageview to image in Objective C who contain an UIView

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

Capture image from UIView is not working on iOS 10.3.3

I capture image from UIView content like below and it's working fine but when i run it in my iPhone5S with iOS 10.3.3, Captured image contains only Black View.
-(UIImage *)captureImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
What's the issue?
Thanks in Advance.
You can do that way as explained by #LGP.
Use following function on main thread like below.
-(UIImage *)captureImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Main thread code.
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *imgResult = [self captureImageFromView:yourView];
});
Try using drawViewHierarchyInRect instead. If this doesn't work either then the problem is likely that you capture the wrong view or after it has been altered in some way.
-(UIImage *)captureImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
FOR SWIFT
func snapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, UIScreen.main.scale)
self.view.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}
FOR OBJECTIVE C
- (UIImage *)captureView {
UIGraphicsBeginImageContext(yourview.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextFillRect(ctx, yourview.frame);
[yourview.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(#"size %f %f",newImage.size.height,newImage.size.width);
return newImage;
}
Before call hide button etc if you need then that will not show on image also.

How to take screen shot in iOS device programmatically for SOS?

Using new API's like
snapshotViewAfterScreenUpdates
resizableSnapshotViewFromRect
drawViewHierarchyInRect
Using these i want to take photo in any format and need to send.
try this
UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
UIImage *snapshotImage = [self imageFromView:screenshotView];
- (UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

Capturing Screenshot of UIVIew with multiple CALayers displays white background while save image in gallery

I have a UIView with different CALayers with different size.
When I am trying to save UIView as image to gallery it looses its transparency:
My code is:
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//Getting image
UIImage *img=[self imageWithView:MainView];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
Note:When I debug the code, it displays transparent image, but when I see in gallery it displays with white Background
I've tried your code. You can try following implementation:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *tmpView = [UIView new];
tmpView.frame = self.view.bounds;
tmpView.backgroundColor = [UIColor clearColor];
[self.view addSubview:tmpView];
UIImage *img = [self imageWithView:tmpView];
[self saveInJPGFormat:img];
[self saveInPNGFormat:img];
}
- (void)saveInJPGFormat:(UIImage *)image {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
- (void)saveInPNGFormat:(UIImage *)image {
NSData* imageData = UIImagePNGRepresentation(image);
UIImage* pngImage = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);
}
- (UIImage *) imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Your method saves as JPG to camera roll. JPGs aren't capable to keep alpha channel.
Second method taken from https://stackoverflow.com/a/10279075/849616 saves image as PNG. I can open it and I can see through the image (alpha channel is saved).
BTW: ofc that's very dirty and quick code. In reality you should do a category on UIImage for these methods. Also keep MVC and either views&layers stick to storyboards or to separate UIView subclass.
Images from my photo gallery:
Screen of preview so you're sure it's empty:
So the method is working.

Crop taken screenshot and save 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;
}

Resources