在保持比例的同时,以612x612的尺寸将完整图像适合Instagram [英] Fit FULL image in 612x612 size for Instagram while keeping ratio
问题描述
我希望能够将612x612 UIImage中的任何完整图像都适合在我的应用中进行Instagram集成.但是我不希望任何侧面割伤或其他任何东西.我希望整个图像在横向或纵向上都能完全适合612x612图像.即时通讯就像是您可以设置图像内容模式以适合图像视图的宽高比,以寻找一种可以调整为我想要的尺寸(612x612)并保持其确切比例的方法.
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.
用户通过uiimageview从那里选择一个现有图像,我希望该图像被选择以612图像的尺寸调整为612,同时保持相同的比例,但仍完全位于612x612范围内,此处是我想要的图像的示例调整为正方形图像时要看的长肖像图像.
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.
我的instagram集成代码.
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");
}
}
但是我得到了这个
这是我用于调整大小的方法的方法,几乎适用于横向查看的图像,而非纵向视图
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;
}
推荐答案
这将创建全屏图像,以便在Instagram上发布高度大于宽度的图像.
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];
要对宽度大于其高度的图像进行此操作,只需将CGSize size
设置为originalImages.size.width
.然后将其居中放置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.
这篇关于在保持比例的同时,以612x612的尺寸将完整图像适合Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!