簡體   English   中英

在 UIImage 中改變只是 .scale?

[英]Changing JUST .scale in 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
        
        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 上! (也許 Apple 的照片確實知道在將其發送到 Instagram 之類的地方之前最好將其縮放為 1!)。

正如您所說, UIImagescale屬性是只讀的 - 因此您無法直接更改它。

但是,使用UIImageinit(cgImage:scale:orientation)初始化器並不真正復制圖像 - 它包裝的底層CGImage (包含實際的位圖數據)仍然是同一個實例。 它只是一個新的UIImage包裝器。

雖然如此,在這種情況下,您可以通過直接通過CGContextmakeImage()方法從上下文中獲取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:))
}

順便說一句,您可以更改結果圖像的比例,從而創建新圖像

let newScaleImage = UIImage(cgImage: oldScaleImage.cgImage!, scale: 1.0, orientation: oldScaleImage.imageOrientation)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM