gpt4 book ai didi

iphone - ios 调整拇指的图像大小 - 虽然高插值但模糊

转载 作者:行者123 更新时间:2023-11-29 11:05:11 26 4
gpt4 key购买 nike

我在 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 ;
}

顺便说一句:使用或不使用高插值没有区别。也许我做错了什么。

预先感谢您的帮助!

最佳答案

制作缩略图的最佳方法是让系统使用图像 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);

关于iphone - ios 调整拇指的图像大小 - 虽然高插值但模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13990745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com