【问题标题】:Fit FULL image in 612x612 size for Instagram while keeping ratio适合 Instagram 的 612x612 尺寸的完整图像,同时保持比例
【发布时间】:2015-02-18 17:25:55
【问题描述】:

我希望能够将任何完整图像放入 612x612 UIImage 中,以便在我的应用中集成 Instagram。但我不想要任何侧面切割或任何东西。我希望完整图像在横向或纵向中完全适合 612x612 图像。我所追求的就像您可以将图像内容模式设置为适合图像视图的纵横比,我正在寻找一种可以调整到我想要的大小(612x612)同时保持其精确比例的方法。

用户通过 uiimageview 从那里选择一个现有图像调整为方形图像时要查看的长纵向图像。

我的 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;
}

【问题讨论】:

  • 你能包含一些代码来审查吗?
  • 我发布了一些几乎适用于风景照片但不适用于肖像图像的代码@KalelWade

标签: ios uiimage instagram resize-image contentmode


【解决方案1】:

这将为高度大于宽度的图像创建一个全屏图像,以便在 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 居中,只需更改等式以计算高度而不是宽度。

【讨论】:

  • 适用于纵向图像,但它会在横向图像的侧面切掉一点
  • @4GetFullOf 您没有完整阅读我的回答吗?它的底部解释了如何处理风景图像。设置 if 语句检查宽度是否大于高度并适当更改代码以处理它...
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 2019-06-12
    • 2020-06-17
    • 2017-05-21
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多