问题
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;
}
回答1:
This will create a full screen image for posting on Instagram for images that have heights greater than their widths.
UIImage *originalImage = [UIImage imageNamed:@"lion.jpg"];
// Create rect with height and width equal to originalImages height
CGSize size = CGSizeMake(originalImage.size.height, originalImage.size.height);
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
// Fill the background with some color
[[UIColor lightGrayColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
// Create image for what we just did
UIImage *bgImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(bgImage.size);
// Take originalImage and center it in bgImage
// We get the center of bgImage by (bgImage.size.width / 2)
// But the coords of the originalImage start at the top left (0,0)
// So we need to subtract half of our orginalImages width so that it centers (bgImage.size.width / 2) - (originalImage.size.width / 2)
[originalImage drawInRect:CGRectMake((bgImage.size.width / 2) - (originalImage.size.width / 2), 0, originalImage.size.width, originalImage.size.height)];
// Create image for what we just did
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Set your finalImage to a UIImageView or send it to Instagram
[self.final setImage:finalImage];
To make this work for images with widths greater than their heights you would just set the CGSize size
to originalImages.size.width
. Then to center it for the finalImage
just change the equation to calculate for height rather than width.
回答2:
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
UIImageView *imageview = [[UIImageView alloc] init];
imageview.contentMode = UIViewContentModeScaleAspectFit;
imageview.frame = CGRectMake(0, 0, 612, 612);
imageview.clipsToBounds = NO;
imageview.image = viewImage;
CGSize imageSize = CGSizeMake(imageview.bounds.size.width, imageview.bounds.size.height);
UIGraphicsBeginImageContext(imageSize);
[imageview.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
来源:https://stackoverflow.com/questions/28589899/fit-full-image-in-612x612-size-for-instagram-while-keeping-ratio