我在保存许多图像时遇到问题。我正在使用UIGraphicsGetImageFromCurrentImageContext。例如,我的输入图像是200kb,输出图像只有35kb。如何在不损失质量的情况下保存图像。这是代码示例:
BOOL first = YES;
mSavedImage = mImageView.image;
UIGraphicsBeginImageContextWithOptions(mImageView.frame.size, YES, 0.0);
DLog(@" FRAME %@",NSStringFromCGSize(mImageView.frame.size));
DLog(@"%i",mEditingView.subviews.count);
for (NSInteger i = mEditingView.subviews.count-1; i>=0; i--){
if ([[mEditingView.subviews objectAtIndex:i] isKindOfClass:[FSShopItem class]]) {
DLog(@"is shopitem");
FSShopItem *lPrewSubview = [mEditingView.subviews objectAtIndex:i];
FSShopItem *lSubview = nil;
if (first) {
[mImageView.image drawInRect:mImageView.frame];
first = NO;
}
else{
lSubview = [mEditingView.subviews objectAtIndex:i+1];
[lSubview.image drawInRect:lSubview.frame];
}
DLog(@" FRAME %@",NSStringFromCGRect(lPrewSubview.frame));
[lPrewSubview.image drawInRect:lPrewSubview.frame];
}
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIImage *lImageForLogo = [UIImage imageNamed:@"ipad_logo.png"];
CGRect lRect = CGRectMake(mEditingView.frame.size.width - lImageForLogo.size.width, mEditingView.frame.size.height - lImageForLogo.size.height, lImageForLogo.size.width, lImageForLogo.size.height);
[lImageForLogo drawInRect:lRect];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
UIImage *lImageForLogo = [UIImage imageNamed:@"iphone_logo.png"];
CGRect lRect = CGRectMake(mEditingView.frame.size.width - lImageForLogo.size.width, mEditingView.frame.size.height - lImageForLogo.size.height, lImageForLogo.size.width, lImageForLogo.size.height);
[lImageForLogo drawInRect:lRect];
}
mSavedImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return mSavedImage;
答案 0 :(得分:1)
我假设mImageView.frame.size
矩形远小于logo.png
图像,因此它们绘制的矩形比原始尺寸小。你需要做的是创建一个更大的上下文 - 比如大4或8倍。现在核心图像将有更多的细节(但当然也更大)。然后,您可以在较小的矩形中渲染此图像,并丢失分辨率,或以原始大小显示该图像以获得完全分辨率。
这里的关键点是你正在使用像素 - 而不是矢量艺术 - 所以如果你用比原始尺寸更小的矩形绘制图像,你将失去细节。
答案 1 :(得分:0)
不是直接使用图像视图帧,而是决定结果图像应该有多大,然后根据屏幕大小(帧坐标空间)和完整之间的缩放因子,使用CGRectInset
变异帧。大小