我使用this post中引入的方法,为Swift修改:
func getRaster() -> [UIColor] {
let result = NSMutableArray()
let img = self.CGImage
let width = CGImageGetWidth(img)
let height = CGImageGetHeight(img)
let colorSpace = CGColorSpaceCreateDeviceRGB()
var rawData = [UInt8](count: width * height * 4, repeatedValue: 0)
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * width
let bytesPerComponent = 8
let bitmapInfo = CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue
let context = CGBitmapContextCreate(&rawData, width, height, bytesPerComponent, bytesPerRow, colorSpace, bitmapInfo)
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), img);
for x in 0..<width {
for y in 0..<height {
let byteIndex = (bytesPerRow * x) + y * bytesPerPixel
let red = CGFloat(rawData[byteIndex] ) / 255.0
let green = CGFloat(rawData[byteIndex + 1]) / 255.0
let blue = CGFloat(rawData[byteIndex + 2]) / 255.0
let alpha = CGFloat(rawData[byteIndex + 3]) / 255.0
let color = UIColor(red: red, green: green, blue: blue, alpha: alpha)
result.addObject(color)
}
}
return (result as NSArray) as! [UIColor]
}
但问题是,如果图像是方形(即32x32精灵),它只会成功,每当我试图获得图像的“光栅”时,尺寸不相等,我得到一个“索引超出范围“x = 16和y = 0时red
的致命错误(对于大小为h的图像:16,w:32)。什么可以解决这个问题?
提前谢谢!
答案 0 :(得分:1)
这很容易解决,实际上,这是问题部分:
Call to undefined method Illuminate\Database\Query\Builder::dispatch() (View: /Users/mereeva/sites/rdb-ws/resources/views/users/trips/status.blade.php)
以下是应该如何:
for x in 0..<width {
for y in 0..<height {
let byteIndex = (bytesPerRow * x) + y * bytesPerPixel
我认为这不需要任何进一步的解释。