保存在相机胶卷中时,PNG图片会丢失质量

时间:2014-01-23 23:23:12

标签: ios iphone xcode image

在我的应用中,我绘制了PNG图片,我想将其保存在相机胶卷中,以便与其他应用分享该图片。但是,保存的图片会失去质量。下面有一个示例来说明问题。 - 左:图片保存在系统/内存中(质量好)。 - 中间:图片保存在相机胶卷中并发送到推特。 - 右:图片保存在相机胶卷中并由gmail发送。除了丢失的质量,扩展改为jpg。

artifacts

我的代码,

- (void)viewDidLoad
{
[super viewDidLoad];

//The sample of draw
UIGraphicsBeginImageContextWithOptions(imageView.frame.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 1.0f);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextAddRect(context, imageView.frame);
CGContextFillPath(context);
CGContextMoveToPoint(context, 40, 40);
CGContextAddLineToPoint(context, 40, 120);
CGContextAddLineToPoint(context, 120, 40);
CGContextAddLineToPoint(context, 40, 40);
CGContextMoveToPoint(context, 200, 120);
CGContextAddLineToPoint(context, 200, 200);
CGContextAddLineToPoint(context, 120, 200);
CGContextAddLineToPoint(context, 200, 120);
CGContextAddLineToPoint(context, 120, 40);
CGContextAddLineToPoint(context, 120, 200);
CGContextAddLineToPoint(context, 40, 120);
CGContextStrokePath(context);

imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Saving in directory on iPhone
NSData *data = UIImagePNGRepresentation(imageView.image);
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"Sample.png"];
[data writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);

//Save in camera roll
UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], nil, nil, nil);
}

所以,我请求你的帮助,以避免损失质量或更好的分享图片的方式。

0 个答案:

没有答案
相关问题