PNG validation on iOS

前端 未结 4 1598
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-11 06:10

Writing a mapping application on iOS, making use of OpenStreetMap tiles. Map tile images are downloaded asynchronously and stored in a dictionary, or persisted in a SQLite DB.

4条回答
  •  醉话见心
    2021-02-11 06:32

    The Swift Version
    func checkPNGImageDataFormat(_ imageData:Data) -> Bool
        {
            //More expensive since it has to go through entire data
            //Check entire header magic number and IEND trailer in PNG data
            var status:Bool = true
            if(imageData.count < 12)
            {
                return false
            }
            let totalBytes = imageData.count
            let bytes = imageData.withUnsafeBytes {
                [UInt8](UnsafeBufferPointer(start: $0, count: totalBytes))
            }
            let header:Bool = bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4e && bytes[3] == 0x47 && bytes[4] == 0x0d && bytes[5] == 0x0a  && bytes[6] == 0x1a && bytes[7] == 0x0a
    
            let iend:Bool = bytes[totalBytes - 12] == 0x00 && bytes[totalBytes - 11] == 0x00 && bytes[totalBytes - 10] == 0x00 && bytes[totalBytes - 9] == 0x00 && bytes[totalBytes - 8] == 0x49 && bytes[totalBytes - 7] == 0x45 && bytes[totalBytes - 6] == 0x4e && bytes[totalBytes - 5] == 0x44 && bytes[totalBytes - 4] == 0xae && bytes[totalBytes - 3] == 0x42 && bytes[totalBytes - 2] == 0x60 && bytes[totalBytes - 1] == 0x82
    
            status = header && iend
            return status
        }
    

提交回复
热议问题