gpt4 book ai didi

ios - 线程 1 : EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

转载 作者:行者123 更新时间:2023-11-30 13:02:32 27 4
gpt4 key购买 nike

在我的 swift 项目中,我有两个类一起工作来保存图像的像素值,以便能够修改红色、绿色、蓝色和 alpha 值。安UnsafeMutableBufferPointer包含许多由 Pixel 类对象组成的咬合。

我可以与持有 UnsafeMutableBufferPointer<Pixel> 的类进行交互属性(property)。我可以访问该对象的所有属性,并且一切正常。我遇到的唯一问题是 UnsafeMutableBufferPoint<Pixel>试图用我的 Pixel 对象循环遍历它,但线程 1 一直崩溃:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)异常。

init!(image: UIImage)
{
_width = Int(image.size.width)
_height = Int(image.size.height)

guard let cgImage = image.cgImage else { return nil }

_width = Int(image.size.width)
_height = Int(image.size.height)
let bitsPerComponent = 8

let bytesPerPixel = 4
let bytesPerRow = _width * bytesPerPixel
let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: _width * _height)
let colorSpace = CGColorSpaceCreateDeviceRGB()

var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
guard let imageContext = CGContext(data: imageData, width: _width, height: _height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
imageContext.draw(cgImage, in: CGRect(origin: CGPoint.zero, size: image.size))

_pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: _width * _height)
}

该函数是导致程序崩溃的部分。崩溃的确切部分是循环遍历 rgba.pixels 的 for 循环。 rgba.pixels 是 UnsafeMutableBufferPointer。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let image: UIImage = info[UIImagePickerControllerEditedImage] as! UIImage!

let rgba = RGBA(image: image)!

for pixel in rgba.pixels
{
print(pixel.red)
}

self.dismiss(animated: true, completion: nil);
}

这是我创建 UnsafeMutableBufferPointer<Pixel> 的构造函数。有没有更简单的方法来做到这一点,并且仍然能够获取 RBGA 值并轻松更改它们。

Pixel 类是 UInt32值被分成四个 UInt 8值。

我是否使用了错误的构造来保存这些值,如果是,是否有更安全或更容易使用的构造?或者我在访问像素值时做错了什么?

最佳答案

这就是我获取图像像素的方法 -

// Grab and set up variables for the original image
let inputCGImage = inputImage.CGImage
let inputWidth: Int = CGImageGetWidth(inputCGImage)
let inputHeight: Int = CGImageGetHeight(inputCGImage)

// Get the colorspace that will be used for image processing (RGB/HSV)
let colorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()!

// Hardcode memory variables
let bytesPerPixel = 4 // 32 bits = 4 bytes
let bitsPerComponent = 8 // 32 bits div. by 4 components (RGBA) = 8 bits per component
let inputBytesPerRow = bytesPerPixel * inputWidth

// Get a pointer pointing to an allocated array to hold all the pixel data of the image
let inputPixels = UnsafeMutablePointer<UInt32>(calloc(inputHeight * inputWidth, sizeof(UInt32)))

// Create a context to draw the original image in (aka put the pixel data into the above array)
let context: CGContextRef = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight, bitsPerComponent, inputBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue | CGBitmapInfo.ByteOrder32Big.rawValue)!

CGContextDrawImage(context, CGRect(x: 0, y: 0, width: inputWidth, height: inputHeight), inputCGImage)

请记住,这不是 Swift 3 语法(如果您使用的是 Swift 3 语法),但这是基本算法。现在要获取每个像素的单独颜色值,您必须实现这些函数 -

func Mask8(x: UInt32) -> UInt32
{
return x & 0xFF
}

func R(x: UInt32) -> UInt32
{
return Mask8(x)
}

func G(x: UInt32) -> UInt32
{
return Mask8(x >> 8)
}

func B(x: UInt32) -> UInt32
{
return Mask8(x >> 16)
}

func A(x: UInt32) -> UInt32
{
return Mask8(x >> 24)
}

要在处理 RGBA 值后创建全新的颜色,请使用此函数 -

func RGBAMake(r: UInt32, g: UInt32, b: UInt32, a: UInt32) -> UInt32
{
return (Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24)
}

要迭代像素数组,您可以这样做 -

var currentPixel = inputPixels
for _ in 0..<height
{
for i in 0..<width
{
let color: UInt32 = currentPixel.memory
if i < width - 1
{
print(NSString(format: "%3.0f", R(x: color), terminator: " "))
}
else
{
print(NSString(format: "%3.0f", R(x: color)))
}
currentPixel += 1
}
}

关于ios - 线程 1 : EXC_BAD_ACCESS (code=EXC_I386_GPFLT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761731/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com