粘贴板UIImage不使用比例 [英] Pasteboard UIImage not using scale

查看:124
本文介绍了粘贴板UIImage不使用比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建自定义键盘,无法将图像添加到粘贴板,并且无法在粘贴的图像中保持适当的比例和分辨率.让我从键盘屏幕快照开始,以说明我的问题:

I am building a custom keyboard and am having trouble adding an image to the pasteboard and maintaining the appropriate scale and resolution with in the pasted image. Let me start with a screenshot of the keyboard to illustrate my trouble:

因此,键盘左上方的脸部图片只是一个UIButton,原始照片设置为背景.按下按钮后,图像将使用以下功能调整大小:

So the picture of the face in the top left of the keyboard is just a UIButton with the original photo set to the background. When the button is pressed the image is resized with the following function:

func imageResize(image:UIImage, size:CGSize)-> UIImage {

    let scale  = UIScreen.mainScreen().scale

    UIGraphicsBeginImageContextWithOptions(size, false, scale)
    var context = UIGraphicsGetCurrentContext()

    CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
    image.drawInRect(CGRect(origin: CGPointZero, size: size))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return scaledImage
}

此函数使用适当的比例创建与UIButton相同大小的UIImage,以反映设备的屏幕分辨率.为了验证该功能是否正确,我添加了一个UIImageView,其中填充了缩放后的图像.缩放后的图像是看起来放在键盘中心附近的图像.我使用以下功能添加了UIImageView:

This function creates a UIImage the same size as the UIButton with the appropriate scale to reflect the device's screen resolution. To verify that the function is correct, I added an UIImageView filled with the scaled image. The scaled image is the image that looks misplaced near the center of the keyboard. I added the UIImageView with this function:

func addImageToBottomRight(image: UIImage) {
    var tempImgView = UIImageView(image: image)
    self.view.addSubview(tempImgView)
    tempImgView.frame.offset(dx: 100.0, dy: 50.0)
}

我尝试了几种不同的方法将图像添加到粘贴板,但是所有人似乎都忽略了图像的比例,而是将其显示为两倍于较大的分辨率:

I have tried a few different methods for adding the image to the pasteboard, but all seem to ignore the scale of the image and display it twice as large as opposed to displaying it at a higher resolution:

let pb = UIPasteboard.generalPasteboard()!

var pngType = UIPasteboardTypeListImage[0] as! String
var jpegType = UIPasteboardTypeListImage[2] as! String

pb.image = image
pb.setData(UIImagePNGRepresentation(image), forPasteboardType: pngType)
pb.setData(UIImageJPEGRepresentation(image, 1.0), forPasteboardType: jpegType)

所有这三种方法均无法正常工作,并且产生的效果与屏幕截图所示相同.有人对其他方法有什么建议吗?为了进一步阐明我的目标,我希望消息文本框中的图像在大小和分辨率方面看起来都与键盘中的两个UIImage相同.

All three of these methods do not work correctly and produce the same result as illustrated in the screenshot. Does anyone have any suggestions of other methods? To further clarify my goal, I would like the image in the message text box to look identical to both UIImages in the keyboard in terms of size and resolution.

以下是UIImage的一些属性,如果有人好奇,请调整大小:

Here are a few properties of the UIImage before and resize in case anyone is curious:

Starting Image Size is (750.0, 750.0)
Size to scale to is: (78.0, 78.0))
Initial Scale: 1.0

Resized Image Size is (78.0, 78.0)
Resized Image Scale: 2.0

推荐答案

我知道这是一篇过时的文章,但是我认为我分享了我在复制图像并将其粘贴到消息传递应用程序这一特定情况下发现的工作. ,当您使用iMessages,whatsapp,messenger等应用程序发送图片时,它们显示图像的方式是使其宽高比适合某些特定的水平宽度(对于此演示来说,大约为260 pts).

I know this is an old post, but thought I share the work around I found for this specific case of copying images and pasting to messaging apps.The thing is, when you send a picture with such apps like iMessages, whatsapp, messenger, etc, the way they display the image is so that it aspect fits to some certain horizontal width (lets say around 260 pts for this demo).

从下图可以看到,如果以150x150的分辨率发送1x分辨率的图像,则图像将被拉伸并显示在所需的260宽度框中,从而使图像呈颗粒状.

As you can see from the diagram below, if you send 150x150 image @1x resolution in imessage, it will be stretched and displayed in the required 260 width box, making the image grainy.

但是,如果您在图像的左侧和右侧都添加了宽度为185的空白边距,则最终将得到尺寸为520x150的图像.现在,如果您在发送该尺寸的图像时遇到了麻烦,则必须将其放入260宽度的框中,最终将520x150的图像塞进260x75的框中,从而以@ 2x的分辨率获得75x75的图像.

But if you add an empty margin of width 185 to both the left and right sides of the image, you will end up with an image of size 520x150. Now if you send that sized image in imessage, it will have to fit it in a 260 width box, ending up cramming a 520x150 image in a 260x75 box, in a way giving you a 75x75 image at @2x resolution.

您可以使用以下代码向UIImage添加清晰的颜色边距

You can add a clear color margin to a UIImage with a code like this

extension UIImage {
    func addMargin(withInsets inset: UIEdgeInsets) -> UIImage? {
         let finalSize = CGSize(width: size.width + inset.left + inset.right, height: size.height + inset.top + inset.bottom)
         let finalRect = CGRect(origin: CGPoint(x: 0, y: 0), size: finalSize)

            UIGraphicsBeginImageContextWithOptions(finalSize, false, scale)
            UIColor.clear.setFill()
            UIGraphicsGetCurrentContext()?.fill(finalRect)

            let pictureOrigin = CGPoint(x: inset.left, y: inset.top)
            let pictureRect = CGRect(origin: pictureOrigin, size: size)

            draw(in: pictureRect)
            let finalImage = UIGraphicsGetImageFromCurrentImageContext()
            defer { UIGraphicsEndImageContext() }

            return finalImage
 }

}

这篇关于粘贴板UIImage不使用比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆