How to check if a uiimage is blank? (empty, transparent)

后端 未结 6 2055
说谎
说谎 2020-12-31 23:07

which is the best way to check whether a UIImage is blank?
I have this painting editor which returns a UIImage; I don\'t want to save this imag

6条回答
  •  梦毁少年i
    2020-12-31 23:20

    Here's a solution in Swift that does not require any additional frameworks.

    Thanks to answers in a related question here: Get Pixel Data of ImageView from coordinates of touch screen on xcode?

    func imageIsEmpty(_ image: UIImage) -> Bool {
        guard let cgImage = image.cgImage,
              let dataProvider = cgImage.dataProvider else
        {
            return true
        }
    
        let pixelData = dataProvider.data
        let data: UnsafePointer = CFDataGetBytePtr(pixelData)
        let imageWidth = Int(image.size.width)
        let imageHeight = Int(image.size.height)
        for x in 0..

提交回复
热议问题