Iphone ios为拇指调整图像大小-模糊但插值较高
我在Iphone ios为拇指调整图像大小-模糊但插值较高,iphone,ios,ipad,uiimage,image-resizing,Iphone,Ios,Ipad,Uiimage,Image Resizing,我在UIImage上写了一个分类,可以调整图像大小,比如iphone照片应用程序 虽然插值设置为高,但图像看起来并不像照片应用程序中的图像。相反,它看起来不锐化或模糊 以下是我所做的: - (UIImage *)resizeImageProportionallyIntoNewSize:(CGSize)newSize; { CGFloat scaleWidth = 1.0f; CGFloat scaleHeight = 1.0f; NSLog(@"Orig
UIImage
上写了一个分类,可以调整图像大小,比如iphone照片应用程序
虽然插值设置为高,但图像看起来并不像照片应用程序中的图像。相反,它看起来不锐化
或模糊
以下是我所做的:
- (UIImage *)resizeImageProportionallyIntoNewSize:(CGSize)newSize;
{
CGFloat scaleWidth = 1.0f;
CGFloat scaleHeight = 1.0f;
NSLog(@"Origin Size = %@", NSStringFromCGSize(self.size));
if (CGSizeEqualToSize(self.size, newSize) == NO) {
//calculate "the longer side"
if(self.size.width > self.size.height) {
scaleWidth = self.size.width / self.size.height;
} else {
scaleHeight = self.size.height / self.size.width;
}
}
// now draw the new image in a context with scaling proportionally
UIImage *sourceImage = self;
UIImage *newImage = nil;
//now we create a context in newSize and draw the image out of the bounds of the context to get
//an proportionally scaled image by cutting of the image overlay
if([[UIScreen mainScreen] scale] == 2.00) {
UIGraphicsBeginImageContextWithOptions(newSize, YES, 2.0);
}
UIGraphicsBeginImageContext(newSize);
// Set the quality level to use when rescaling
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
//Center image point so that on each egde is a little cutoff
CGRect thumbnailRect = CGRectZero;
thumbnailRect.size.width = (int) newSize.width * scaleWidth;
thumbnailRect.size.height = (int) newSize.height * scaleHeight;
thumbnailRect.origin.x = (int) (newSize.width - thumbnailRect.size.width) * 0.5;
thumbnailRect.origin.y = (int) (newSize.height - thumbnailRect.size.height) * 0.5;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@"could not scale image");
return newImage ;
}
顺便说一句:使用高插值与否没有区别。也许我做错了什么
提前感谢您的帮助 如果不锐化/模糊,应尝试通过以下方式关闭抗锯齿:
CGContextSetShouldAntialias(context, NO);
希望这能有所帮助。制作缩略图的最佳方法是让系统使用图像I/O框架为您制作缩略图。唯一的诀窍是,由于您使用的是CGImage,因此必须考虑屏幕分辨率:
CGImageSourceRef src = CGImageSourceCreateWith... // whatever
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat w = // maximum size, multiplied by the scale
NSDictionary* d =
[NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
(id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
[NSNumber numberWithInt:(int)w], kCGImageSourceThumbnailMaxPixelSize,
nil];
CGImageRef imref =
CGImageSourceCreateThumbnailAtIndex(src, 0, (__bridge CFDictionaryRef)d);
UIImage* im =
[UIImage imageWithCGImage:imref scale:scale orientation:UIImageOrientationUp];
CFRelease(imref); CFRelease(src);
请不要使用“[[UIScreen mainScreen]比例]==2.00”。它可能无法正常工作。我建议使用高分辨率图像或矢量格式。@RichardJ.RossIII我为什么要使用高分辨率图像?我正在缩小它!分辨率应该没问题,原始图像大8倍-你读过问题吗?@AlexanderBabaev我应该用什么来代替?行
UIGraphicsBeginImageContextWithOptions(newSize,是的,2.0)代码>无效,因为您输入了行UIGraphicsBeginImageContext(newSize)代码>始终被执行。您可能需要在if
中添加else
。