Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 正在更改。在UIImage中缩放?_Swift_Uiimage - Fatal编程技术网

Swift 正在更改。在UIImage中缩放?

Swift 正在更改。在UIImage中缩放?,swift,uiimage,Swift,Uiimage,在这里,我正在创建一个典型的图形(它的全屏大小,在所有设备上)的飞行 func buildImage()->UIImage { let wrapperA:UIView = say, a picture let wrapperB:UIView = say, some text to go on top let mainSize = basicImage.bounds.size

在这里,我正在创建一个典型的图形(它的全屏大小,在所有设备上)的飞行

func buildImage()->UIImage
        {
        let wrapperA:UIView = say, a picture
        let wrapperB:UIView = say, some text to go on top
                
        let mainSize = basicImage.bounds.size
        
        UIGraphicsBeginImageContextWithOptions(mainSize, false, 0.0)
        
        basicImage.drawHierarchy(in: basicImage.bounds, afterScreenUpdates:true)
        wrapperA.drawHierarchy(in: wrapperA.bounds, afterScreenUpdates:true)
        wrapperB.drawHierarchy(in: wrapperB.bounds, afterScreenUpdates: true)
        
        let result:UIImage? = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        // so we've just created a nice big image for some reason,
        // no problem so far

        print( result?.scale  )
        
        // I want to change that image to have a scale of 1.
        // I don't know how to do that, so I actually just
        // make a new identical one, with scale of 1
        
        let resultFixed:UIImage = UIImage(cgImage: result!.cgImage!,
                            scale: 1.0,
                            orientation: result!.imageOrientation)
        
        print( resultFixed.scale )
        
        print("Let's use only '1-scale' images to upload to things like Instagram")
        
        // return result
        return resultFixed
        
        // be sure to ask on SO if there's a way to
        // just "change the scale" rather than make new.
        }
我需要最终图像为
.scale
的1-但是
.scale
是只读属性

我唯一知道怎么做的就是做一个全新的图像拷贝。。。但在创建时将比例设置为1

有更好的办法吗


小贴士-
其动机是:假设您正在将一张大图像保存到用户的相册中,并且允许UIActivityViewController发布到(例如)Instagram。一般来说,在发送到Instagram(例如)之前,最好将标尺设置为1;如果比例是3,你在Instagram帖子上只得到了图片左上角的1/3。就将其保存到iOS相册而言,将比例设置为1似乎是无害的(也许在某些方面更好)。(我只说“更好”,因为如果图像最终是通过电子邮件发送给PC上的朋友的,那么如果比例为1,则会导致更少的混淆。)有趣的是,如果你只是使用iOS相册,拍摄比例为2或3的图像,并将其共享给Instagram:它实际上在Instagram上正确显示!(也许苹果的照片确实知道,在发送到Instagram这样的地方之前,最好将其缩放为1!)。

正如您所说,
UIImage
scale
属性是只读的,因此您无法直接更改它

但是,使用
UIImage
init(cgImage:scale:orientation)
initialiser并没有真正复制图像–它所包装的底层
cgImage
仍然是相同的实例(其中包含实际的位图数据)。它只是一个新的
UIImage
包装器

尽管如此,在本例中,您可以通过
CGContext
的方法直接从上下文中获取
CGImage
,从而切断中间的
UIImage
包装。例如:

func buildImage() -> UIImage? {

    // ...

    let mainSize = basicImage.bounds.size

    UIGraphicsBeginImageContextWithOptions(mainSize, false, 0.0)
    defer {
        UIGraphicsEndImageContext()
    }

    // get the current context
    guard let context = UIGraphicsGetCurrentContext() else { return nil }

    // -- do drawing here --

    // get the CGImage from the context by calling makeImage() – then wrap in a UIImage
    // through using Optional's map(_:) (as makeImage() can return nil)
    // by default, the scale of the UIImage is 1.
    return context.makeImage().map(UIImage.init(cgImage:))
}

作为旁注,我建议不要强制展开生成的图像,而是处理无法创建图像的情况。在本例中,我将向调用者返回
nil
。(2)啊。。你的意思是,让结果:UIImage?=UIGraphicsGetImageFromCurrentImageContext()。。。事实上,我可以在返回CGImage的当前上下文上使用这个“makeImage()”,然后使用UIImage.init将其包装为UIImage。。。好极了@乔埃布洛:)知识渊博的回答,再次感谢@Hamish