ios - 缩小 UIImage 并保持清晰

标签 ios swift uiimage swift2 uigraphicscontext

我正在使用 this 的答案缩小图像并使其不模糊的问题。但是,这对我不起作用。我正在使用 swift 。我按照答案中的方式做所有事情。这是我使用的函数的代码:

func resizeImage(image: UIImage, newSize: CGSize) -> (UIImage) {
        let newRect = CGRectIntegral(CGRectMake(0,0, newSize.width, newSize.height))
        let imageRef = image.CGImage

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
        let context = UIGraphicsGetCurrentContext()

        // Set the quality level to use when rescaling
        CGContextSetInterpolationQuality(context, CGInterpolationQuality.High)
        let flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height)

        CGContextConcatCTM(context, flipVertical)
        // Draw into the context; this scales the image
        CGContextDrawImage(context, newRect, imageRef)

        let newImageRef = CGBitmapContextCreateImage(context)! as CGImage
        let newImage = UIImage(CGImage: newImageRef)

        // Get the resized image from the context and a UIImage
        UIGraphicsEndImageContext()

        return newImage
    }

这就是我所说的:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        let identifier = "mypin"

        if annotation.isKindOfClass(MKUserLocation) {
            return nil
        }

        let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil
        {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView!.canShowCallout = true
            let pinImage = UIImage(named: "Pin.png")
            let resizedImage = resizeImage(pinImage!, newSize: CGSize(width: 12, height: 20))
            annotationView?.image = resizedImage
            annotationView!.rightCalloutAccessoryView = detailButton
        }
        else
        {
            annotationView!.annotation = annotation
        }

        return annotationView
    }

如果有人知道为什么会发生这种情况,我将不胜感激。 提前致谢。

最佳答案

正如我在评论中所怀疑的那样,这是因为 CGBitmapContextCreateImage 将忽略您输入到 UIGraphicsBeginImageContextWithOptions 中的比例——而是始终创建具有比例的图像1.0

例如,如果您有 3 倍比例的 50 x 50 图像上下文:

  • CGBitmapContextCreateImage 将以 1 倍比例返回 150 x 150 图像

  • UIGraphicsGetImageFromCurrentImageContext 将以 3 倍比例返回 50 x 50 图像

通常这在屏幕上显示图像时实际上应该没有什么区别,除非您依赖以点而不是像素指定的大小。但是,对于 MKAnnotationViewthe documentation says :

Assigning a new image to this property also changes the size of the view’s frame so that it matches the width and height of the new image.

因此,如果您为其分配一个 1 倍比例的图像(并且您的屏幕比例高于 1 倍)——它将被不必要地放大,从而降低质量。因此,解决方法是使用 UIGraphicsGetImageFromCurrentImageContext

关于ios - 缩小 UIImage 并保持清晰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37067880/

相关文章:

ios - Swift 将泛型类型限制为 Type

ios - 如何自动裁剪 UIImage?

iphone - 无法根据 xib 中的按钮更新 UIView 中的 UIImage 变量

ios - 如何将触摸从 UIView 传递到 CCLayer(ccTouchesBegin 等)

iOS在动画过程中显示标签或图像位置

ios - Xcode:上传到 App Store 变灰,但支付了 99 美元

Swift 在 2 bool didset 调用后调用一个方法

swift - UIImage 动画图像缓存的内存问题

iphone - 在通用应用程序中获取主要 Storyboard

java.io.IOException : Stream closed with WS call