我希望能够在 612x612 UIImage 中适合任何完整图像,以便在我的应用程序中集成 Instagram。但我不想要任何侧面切割或任何东西。我希望完整图像在横向或纵向中完全适合 612x612 图像。我所追求的就像你可以将图像内容模式设置为适合图像视图的纵横比,我正在寻找一种可以调整到我想要的大小(612x612)同时保持其精确比例的方法。
用户从那里通过 uiimageview 选择现有图像 我希望将选择的图像调整为 612 x 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;
}