我希望能够在我的应用程序中将任何FULL图像放入612x612 UIImage for Instagram集成中。但我不希望任何方面削减或任何东西。我希望完整的图像在风景或肖像中完全适合612x612图像。即时通讯就像您可以将图像内容模式设置为图像视图中的宽高比适合我正在寻找一种方法,该方法将调整到我想要的大小(612x612),同时保持其精确比例。
用户通过uiimageview从那里选择现有图像我希望选择的图像被调整为612到612图像,同时保持相同的比例但完全在612x612范围内这里是我想要的图像的示例调整大小为方形图像时要查看的长肖像图像。
我的Instagram集成代码..
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;
}
答案 0 :(得分:3)
这将创建一个全屏图像,用于在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
即可。然后以finalImage
为中心,只需更改等式即可计算高度而不是宽度。
答案 1 :(得分:0)
我只是创建一个图像视图,内容模式设置为纵横比拟然后截图图像视图..如果其他人有更优雅的解决方案让我知道
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();