Search code examples
iosuiimageinstagramuigraphicscontextcgsize

How to increase UIImage pixels to post to Instagram


I have captured an image on an app I am creating using the code below. It captures and stores the image perfectly and I can post it to Facebook & Twitter perfectly fine. However, it wont let me post it to Instagram because the image size isn't at least 612px x 612px. Is there any way I can increase the pixel size of my image? It would only need a slight increase as it is already at 568px x 570px. Any advise would be great! Thanks!

Code to get capture image:

CGSize newImageSize = CGSizeMake(self.mainImage.frame.size.width, self.mainImage.frame.size.height);

UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData*theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too

UIImage * imagePNG = [UIImage imageWithData:theImageData]; // wrap UIImage around PNG representation

UIGraphicsEndImageContext();
return imagePNG;

Code to post to Instagram:

//Instagram Image
if (_testImage.image.size.width < 612 || _testImage.image.size.height <612) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Information" message:@"The image you are trying to post to Instagram is too small. Image must be at least 612px x 612px" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    NSLog(@"image size: %@", NSStringFromCGSize(_testImage.image.size));
    [alert show];
}else{
    NSString* imagePath = [NSString stringWithFormat:@"%@/image.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
    [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
    [UIImageJPEGRepresentation(_testImage.image, 0.2) writeToFile:imagePath atomically:YES];
    NSLog(@"image size: %@", NSStringFromCGSize(_testImage.image.size));
    _docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
    _docController.delegate=self;
    _docController.UTI = @"com.instagram.exclusivegram";
    [_docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}

Solution

  • You can scale it, but this will make the quality of your images worse.

    Scale method:

    - (UIImage*)scaleImage:(UIImage*)image withScale:(CGFloat)scale
    {
        CGSize newSize = CGSizeMake(image.size.width*scale, image.size.height*scale);
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }