如何增加UIImage像素以发布到Instagram

时间:2014-08-02 21:11:12

标签: ios uiimage instagram uigraphicscontext cgsize

我使用下面的代码在我正在创建的应用上捕获了一张图片。它完美地捕获和存储图像,我可以将其发布到Facebook& Twitter完全没问题。但是,它不会让我发布到Instagram,因为图像大小不是至少612px x 612px。有什么办法可以增加图像的像素大小吗?它只需要略微增加,因为它已经是568px x 570px。任何建议都会很棒!谢谢!

获取捕获图像的代码:

CGSize newImageSize = CGSizeMake(self.mainImage.frame.size.width, self.mainImage.frame.size.height);

UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0);
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData*theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too

UIImage * imagePNG = [UIImage imageWithData:theImageData]; // wrap UIImage around PNG representation

UIGraphicsEndImageContext();
return imagePNG;

发布到Instagram的代码:

//Instagram Image
if (_testImage.image.size.width < 612 || _testImage.image.size.height <612) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Information" message:@"The image you are trying to post to Instagram is too small. Image must be at least 612px x 612px" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    NSLog(@"image size: %@", NSStringFromCGSize(_testImage.image.size));
    [alert show];
}else{
    NSString* imagePath = [NSString stringWithFormat:@"%@/image.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
    [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
    [UIImageJPEGRepresentation(_testImage.image, 0.2) writeToFile:imagePath atomically:YES];
    NSLog(@"image size: %@", NSStringFromCGSize(_testImage.image.size));
    _docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
    _docController.delegate=self;
    _docController.UTI = @"com.instagram.exclusivegram";
    [_docController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}

1 个答案:

答案 0 :(得分:0)

您可以缩放它,但这会使您的图像质量变差
比例法:

- (UIImage*)scaleImage:(UIImage*)image withScale:(CGFloat)scale
{
    CGSize newSize = CGSizeMake(image.size.width*scale, image.size.height*scale);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}