Search code examples
objective-cuiimageviewuiimagescaleuiactivityviewcontroller

Share A High Resolution Image


Here's my code:

 - (void)shareButtonTapped
 {
    CGSize newImageSize = CGSizeMake(850.0, 850.0);
    UIGraphicsBeginImageContextWithOptions(newImageSize, YES, _aImageView.image.scale);
    [self.aImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    [self.aImageView.image drawInRect:CGRectMake(0, 0, newImageSize.width, newImageSize.height)];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // UIActivityViewController code here
 }

Right now if I tap on the Mail button, for example, the image is the newImageSize, but my _aImageView.image is only displayed in the top left corner while the rest of the image is solid black.

How do I edit this code to scale the image to the 850 x 850 or to a high resolution size so it will look sharp when shared via Email or Social Media?

Thanks!

EDIT *

Still stuck. I feel like I am almost there.

Here's my new code:

 - (void)shareButtonTapped
 {
       _topBarImageView.hidden = YES;
       _bottomBarImageView.hidden = YES;

       CGSize newImageSize = CGSizeMake(600.0, 600.0);
       UIGraphicsBeginImageContextWithOptions(newImageSize, YES, _aImageView.image.scale);
       [_aImageView.layer = renderInContext:UIGraphicsGetCurrentContext()];
       [_aLabel drawTextInRect:CGRectMake(20, 250, 280, 140)];
       UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
       UIImage * newImage = [UIImage imageWithImage:image scaledToSize:newImageSize];
       UIGraphicsEndImageContext();

       _topBarImageView.hidden = NO;
       _bottomBarImageView.hidden = NO;

       // Share Code
 }

So I'm hiding a couple of custom imageViews that are on this screen. The only thing I want shared is the aImageView and the aLabel.

Right now when I open my email for example, I can see the newImageSize, but the majority of the box is still black. I still can't figure out what I am missing.


Solution

  • Please try below code :

    + (UIImage*)imageWithImage:(UIImage*)image 
                   scaledToSize:(CGSize)newSize;
    {
       UIGraphicsBeginImageContext( newSize );
       [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
       UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
    
       return newImage;
    }
    

    specify your new size as (850,850) and pass your original image, but make sure your height and width ratio of original image should be same as converted image ratio.

    Hope it will works for you.Thanks

    EDIT :

    For Capturing screenshot of your view follow below code :

    - (UIImage*)captureView:(UIView *)yourView {
        CGRect rect = [[UIScreen mainScreen] bounds];
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [yourView.layer renderInContext:context];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }