I want to be able to fit any FULL image in a 612x612 UIImage for Instagram integration in my app. But i don't want any of the sides cut or anything. I want the full image weather its in landscape or portrait to fit fully in a 612x612 image. What im after is just like you can set an image content mode to aspect fit in an image view im looking for a method that will resize to my desired size(612x612) while keep its exact ratio.
The user selects an existing image through a uiimageview from there I want that image selected to be resized to 612 by 612 image while keeping the same ratio yet fully being within the 612x612 bounds here an example of what image f what i want a long portrait image to look when resized into a square image.
My instagram integration code..
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
{
UIImage *viewImage;
switch (whichPhoto) {
case 1:
viewImage = photoOneImageView.image;
break;
case 2:
viewImage = photoTwoImageView.image;
break;
case 3:
viewImage = photoThreeImageView.image;
break;
default:
break;
}
//here is where i resize my image
viewImage = [self scaleImage:viewImage toSize:CGSizeMake(612, 612)];
NSData *imageData = UIImagePNGRepresentation(viewImage); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(@"image saved");
CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
NSString *fileNameToSave = [NSString stringWithFormat:@"Documents/insta.igo"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNameToSave];
NSLog(@"jpg path %@",jpgPath);
NSString *newJpgPath = [NSString stringWithFormat:@"file://%@",jpgPath];
NSLog(@"with File path %@",newJpgPath);
NSURL *igImageHookFile = [[NSURL alloc]initFileURLWithPath:newJpgPath];
NSLog(@"url Path %@",igImageHookFile);
self.documentController.UTI = @"com.instagram.exclusivegram";
self.documentController = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.documentController=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
NSString *caption = @"My caption"; //settext as Default Caption
self.documentController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",caption],@"InstagramCaption", nil];
[self.documentController presentOpenInMenuFromRect:rect inView: self.view animated:YES];
}
else
{
NSLog (@"Instagram not found");
}
}
but instead i get this
Here is the method i use for resized works almost for landscape viewed images but not portrait
- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if( image.size.width > image.size.height ) {
scaleFactor = image.size.width / image.size.height;
scaledSize.width = newSize.width;
scaledSize.height = newSize.height / scaleFactor;
}
else {
scaleFactor = image.size.height / image.size.width;
scaledSize.height = newSize.height;
scaledSize.width = newSize.width / scaleFactor;
}
UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
[image drawInRect:scaledImageRect];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
I just create an image view with content mode set to aspect fit then screenshot that image view.. if anyone else has a more elegant solution let me know
This will create a full screen image for posting on Instagram for images that have heights greater than their widths.
To make this work for images with widths greater than their heights you would just set the
CGSize size
tooriginalImages.size.width
. Then to center it for thefinalImage
just change the equation to calculate for height rather than width.