【发布时间】:2014-12-06 04:56:28
【问题描述】:
问题简历:
我用相机拍照或从库中选择一张,然后添加一些UITextFields(在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 image on my server
图片上传到推特:Twitter image on my server
文件图片:Picture from the documents on my server (there is a huge difference!)
那么,有谁知道我应该为代码中的图像选择什么设置以提高质量(即使在 .jpg 压缩之后,它似乎在任何地方都可以完成,甚至在这里)至少在某种程度上更好?在我看来,它只发生在带有文字的纯色背景上(当我尝试上传没有背景的图像时,它看起来更好,但必须有一种方法可以使它在背景下看起来更好)
谢谢!
编辑 事实证明,问题可能不在于共享内容,而在于 jpeg 压缩本身。正如它在答案中所说:Facebook: Ways to preserve image quality of uploaded images?,
“JPEG 不适合带有文本、大块色块或简单形状的图像,因为清晰的线条会模糊,颜色会发生变化。http://graphicssoft.about.com/od/graphicformats/f/summary.htm”
所以我想没有解决办法,因为 Facebook、Twitter 甚至 stack.imgur 都使用 jpeg 压缩,只要有“带有文本、大块颜色或简单形状的图像”,它就会来得很糟糕。太糟糕了。
【问题讨论】:
-
你试过使用
UIImageJPEGRepresentation (UIImage *image, CGFloat compressionQuality);吗?这使您可以通过更改压缩率来更改图像质量。 -
我确实尝试过,但效果不佳。此外,有人会猜测,使用 PNG 表示应该创建一个足够高质量的图像,与压缩质量等于 100% 的 JPEG 表示几乎相同,但它也失败了。
标签: ios objective-c image image-quality socialshare