Search code examples
objective-ciosfacebookuiimagescale

how to scale screen image for posting on Facebook wall?


I'm working on an iPhone app one function of which posts an image of the iPhone screen to the user's Facebook wall, but I'm having trouble sizing and scaling the image properly. When the app posts to Twitter there's no problem, but when it posts to Facebook I end up getting just a small part of a much bigger image. What I want is just a scaled-down version of the screen image.

Here's the code I'm using (or trying to use):

This creates the image.

-(UIImage *)createImage
{
CGRect newRect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-44);
UIGraphicsBeginImageContext(newRect.size); //([self.view frame].size])
[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext([self.view bounds].size);
[myImage drawInRect:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-44)];
myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return myImage;
}

And this currently fails to scale it:

- (UIImage*) scaleImage:(UIImage*)image
{
int widthToUse = [[UIScreen mainScreen] bounds].size.width;
int heightToUse = [[UIScreen mainScreen] bounds].size.height;
CGSize scaledSize;
scaledSize.height = 350;  //I used 350 because it seemed to be the maximum size of a Facebook wall photo.
scaledSize.width = ((350*widthToUse)/heightToUse);
UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
[image drawInRect:scaledImageRect];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

Here's how I'm using it:

    [controller setInitialText:msgText];
    [controller addURL:[NSURL URLWithString:@"http://www.myURL.com"]];
    UIImage *img = [self createImage];
    UIImage *pic;
    if (self.serviceType==SLServiceTypeFacebook)
    {
        pic = [self scaleImage:img];
    }
    else if (self.serviceType==SLServiceTypeTwitter)
    {
        pic = img;
    }
    [controller addImage:pic];
    [self presentViewController:controller animated:YES completion:Nil];

Any thoughts on how I can fix this?

Thanks in advance for your help.


Solution

  • Well, I changed the last number in UIGraphicsBeginImageContextWithOptions in the scaleImage method (from 0.0 to 0.975), and it seems to have done the trick. Oddly, while 0.975 was fine, 0.98 (and higher numbers) replicated the original problem.

    - (UIImage*) scaleImage:(UIImage*)image
    {
    int widthToUse = [[UIScreen mainScreen] bounds].size.width;
    int heightToUse = [[UIScreen mainScreen] bounds].size.height;
    CGSize scaledSize;
    scaledSize.height = 350;
    scaledSize.width = ((350*widthToUse)/heightToUse);
    UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.975 );
    CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
    [image drawInRect:scaledImageRect];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    }