问题简历:图像质量
我采取与相机的照片或选择一个从库,然后(旁UIImageView
与所选择的图像)添加一些UITextField
s到它,创建一个全部都是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
和当我在那里看时,它看起来相当好!这真的让我想知道为什么我不使用带有打印图片的信封(当然是保存在文档中的信封!),并且不要将它发送到CA的Menlo Park和CA的Market of South,而不是尝试解决这个荒谬的问题。
现在来照片!
umh ..所以,当我试图上传图片在这里,上传thingy把.png图像也变成了可怕的质量!啊,我以为stackOverflow和我在一起!
不管怎么说,这里是图片(这里上传),以及相应的链接,这说明实际的图像,我的服务器上(我希望不会破坏它们为好)
图片上传到Facebook的: Facebook image on my server
图片上传到Twitter: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 *图像,CGFloat compressionQuality);'?这使您可以通过更改压缩率来更改图像的质量。 –
我的确确实实在在地尝试过,而且这并不好。此外,人们会猜测使用PNG表示应该创建一个足够高质量的图像,与压缩质量等于百分之百的JPEG表示几乎相同,但它也失败了。 – Gyfis
类似的问题:http://stackoverflow.com/q/10966089/448734 – Bryan