在保持比例的同时为 Instagram 调整 612x612 尺寸的完整图像

Fit FULL image in 612x612 size for Instagram while keeping ratio

我希望能够将任何完整图像放入 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;
}

这将为在 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 的中心,只需更改方程式以计算高度而不是宽度。

我只是创建了一个内容模式设置为纵横比适合的图像视图,然后截取该图像视图。如果其他人有更优雅的解决方案,请告诉我

                    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();