簡體   English   中英

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

[英]How to increase UIImage pixels to post to Instagram

我在使用以下代碼創建的應用程序中捕獲了圖像。 它可以完美地捕獲和存儲圖像,我可以很好地將其發布到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];
}

您可以縮放它,但這會使圖像質量變差。

縮放方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM