Ios 为Instagram安装612x612大小的完整图像,同时保持比例
我希望能够在612x612 UIImage中容纳任何完整图像,以便在我的应用程序中集成Instagram。但我不想切任何边或任何东西。我希望完整的图像天气在风景或肖像完全适合在一个612x612图像。我所追求的就像你可以设置一个图像内容模式,以适应图像视图中的纵横比。我正在寻找一种方法,可以将大小调整到我想要的大小(612x612),同时保持精确的比例 用户通过uiimageview选择现有图像,我希望选择的图像大小调整为612×612图像,同时保持相同的比率,但完全在612x612范围内。这里是一个示例,说明了当将长肖像图像调整为方形图像时,我希望长肖像图像的外观 我的instagram集成代码Ios 为Instagram安装612x612大小的完整图像,同时保持比例,ios,uiimage,instagram,resize-image,contentmode,Ios,Uiimage,Instagram,Resize Image,Contentmode,我希望能够在612x612 UIImage中容纳任何完整图像,以便在我的应用程序中集成Instagram。但我不想切任何边或任何东西。我希望完整的图像天气在风景或肖像完全适合在一个612x612图像。我所追求的就像你可以设置一个图像内容模式,以适应图像视图中的纵横比。我正在寻找一种方法,可以将大小调整到我想要的大小(612x612),同时保持精确的比例 用户通过uiimageview选择现有图像,我希望选择的图像大小调整为612×612图像,同时保持相同的比率,但完全在612x612范围内。这里
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");
}
}
但是我得到了这个
这里是我用于调整大小的作品的方法,几乎用于横向观看的图像,但不用于纵向
- (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上发布高度大于宽度的图像
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
。然后将其置于最终图像的中心,只需更改公式以计算高度而不是宽度。我只需创建一个内容模式设置为aspect fit的图像视图,然后截图显示该图像视图。。如果其他人有更优雅的解决方案,请告诉我
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();
你能提供一些代码来复习吗?我已经发布了一些代码,这些代码几乎适用于风景图片,但不适用于肖像图片@KalelWadeworks,适用于肖像图片,但它在风景图片的侧面略作删减images@4GetFullOf你没有完整地阅读我的答案吗?它的底部解释了如何处理风景图像。设置if语句以检查宽度是否大于高度,并适当更改代码以处理它。。。