问题简述:
我使用相机拍摄照片或从图库中选择一张照片,然后在其上添加一些UITextField
(与所选图片旁边的UIImageView
),创建一个包含所有内容的UIImage
,然后我想要分享它,特别是在Facebook和Twitter上。我能够做到这一点,但是图像质量显著下降。如何调整代码以保持质量最佳?
长描述,包含代码和图片
首先,我在相机和图库之间进行选择。然后,在某种方式下选择图像后,我使用
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
方法获取图像,在该方法中,我将图像分配给我的全局UIImage *chosenImage
,并在之后使用它。
然后,我将chosenImage
添加到UIImageView
中,并添加一个UITextField
作为同级,例如:
UIImageView *chosenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, actualHeight - 50)];
[chosenImageView setBackgroundColor:[UIColor clearColor]];
[chosenImageView setContentMode:UIViewContentModeScaleAspectFill];
[chosenImageView setClipsToBounds:YES];
[chosenImageView setImage:chosenImage];
UITextField *textFieldName = [[UITextField alloc] initWithFrame:CGRectMake(15, 15, 290, 30)];
[textFieldName setBackgroundColor:[UIColor blueColor]];
[textFieldName setDelegate:self];
[textFieldName setTag:-1];
[textFieldName setText:textName];
[textFieldName sizeToFit];
[textFieldName setContentScaleFactor:2.0];
[pageView addSubview:textFieldName];
[pageView addSubview:chosenImageView];
在这之后,我有一个“分享!”的按钮,点击后会出现将图像分享到Facebook和Twitter的可能性。然后我使用此方法获取
UIImage *imageToShare
来将UIView
转换为UIImage
(在我看来似乎运行良好)。- (UIImage *)changeViewToImage:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, 0);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
似乎在Facebook和Twitter上分享的图片质量相同,而这里的“相同”意味着非常糟糕。
我用于Twitter分享的代码:
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@""];
[tweetSheet addImage:imageToShare];
[tweetSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[self presentViewController:tweetSheet animated:YES completion:nil];
我用于Facebook分享的代码:
NSDictionary *params = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:UIImagePNGRepresentation(imageToShare), imageDescription, nil] forKeys:[NSArray arrayWithObjects:@"source", @"message", nil]];
[FBRequestConnection startWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
[[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Your photo was successfully posted to your Facebook Wall" delegate:nil cancelButtonTitle:@"Ok :)" otherButtonTitles:nil, nil] show];
[self returnFromShareView];
}];
然而,我已经添加了一些代码来将imageToShare
保存到文档中:
NSData *data = UIImagePNGRepresentation(imageToShare);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"currentImage.png"]; //Add the file name
[data writeToFile:filePath atomically:YES]; //Write the file
当我在那里查看时,它看起来相当好!这让我想知道为什么我不使用带有印刷图片的信封(当然是保存在文档中的那个!),并将其发送到加州门洛帕克和南市场,而不是试图解决这个荒谬的问题。
现在是图片!
嗯..所以在我尝试在这里上传图像之后,上传工具也将.png图像变成了可怕的质量!啊,我还以为StackOverflow会帮助我的!
无论如何,这些是图片(在此处上传),以及相应的链接,显示在我的服务器上的实际图像(我希望它们也不会被破坏)
上传到Facebook的图片:我的服务器上的Facebook图片
![Facebook图片](https://istack.dev59.com/f1YGj.webp)
![Twitter图片](https://istack.dev59.com/xPfF0.webp)
文档中的图片:我的服务器上的文档图片(有很大的区别!)
那么,有人知道在代码中选择什么设置可以使图像质量(即使在 .jpg 压缩后,似乎到处都是压缩)至少有所提高吗?而且在我看来,这只发生在带有文字的纯色背景上(当我尝试上传没有背景的图像时,它看起来更好,但一定有方法可以使它在有背景的情况下看起来更好)
谢谢!
编辑 事实证明,问题可能不在共享方面,而在于 jpeg 压缩本身。正如答案中所述:Facebook:保留上传图像的图像质量的方法?,
"JPEG不适合带有文本、大块颜色或简单形状的图像,因为清晰的线条会变得模糊,颜色也会发生偏移。http://graphicssoft.about.com/od/graphicformats/f/summary.htm所以我想没有解决方案,因为Facebook、Twitter甚至stack.imgur都使用JPEG压缩,只要还有'带有文本、大块颜色或简单形状的图像',它就会出现问题。太糟糕了。"
UIImageJPEGRepresentation (UIImage *image, CGFloat compressionQuality);
吗?这可以通过改变压缩率来改变图像的质量。 - Ömer Faruk Almalı