有时,CGContextDrawImage会导致“错误访问错误”,执行下面的代码,我们无法找到原因。是否有人在使用"CGContextDrawImage“时遇到过同样的错误?
let bytesPerPixel = 4;
let bytesPerRow:UInt = UInt(bytesPerPixel) * UInt(CG_Width)
let bitsPerComponent:UInt = 8;
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
var pixel = UnsafeMutablePointer<CUnsignedChar>.alloc(4)
let context = CGBitmapContextCreate(pixel,
UInt(CG_Width), UInt(CG_Width), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
let cgRect:CGRect = CGRectMake (0,0,CGFloat(CG_Width),CGFloat(CG_Width)) as CGRect
CGContextDrawImage(context, cgRect, imageRef) 发布于 2015-02-10 17:07:16
当您创建像素缓冲区时,您只分配了4个字节,这对于1x1位图来说就足够了。想必,CG_Width ( CG_Height在哪里使用?)不是1,所以当你欺骗CGBitmapContextCreate关于缓冲区的大小,然后进入它时,你在随机内存中乱写。将缓冲区分配更改为:
var pixel = UnsafeMutablePointer<CUnsignedChar>.alloc(bytesPerRow * CG_Height)然后将上下文创建更改为使用正确的高度:
let context = CGBitmapContextCreate(pixel,
UInt(CG_Width), UInt(CG_Height), bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)如果您实际上是有意创建一个方形位图,请将我的CG_Height更改为CG_Width
https://stackoverflow.com/questions/28432736
复制相似问题