gpt4 book ai didi

ios - 在 Swift 中绘制图像需要很长时间

转载 作者:IT王子 更新时间:2023-10-29 05:27:10 26 4
gpt4 key购买 nike

在我的应用程序中,我正在创建一个图像映射 float 到一个像素值,并将其用作 Google map 上的叠加层,但这需要很长时间才能完成,同样的事情在 Android 中几乎是即时的。我的代码如下所示:

private func imageFromPixels(pixels: [PixelData], width: Int, height: Int) -> UIImage? {

let bitsPerComponent = 8
let bitsPerPixel = bitsPerComponent * 4
let bytesPerRow = bitsPerPixel * width / 8

let providerRef = CGDataProvider(
data: NSData(bytes: pixels, length: height * width * 4)
)

let cgimage = CGImage(
width: width,
height: height,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue),
provider: providerRef!,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent
)

if cgimage == nil {
print("CGImage is not supposed to be nil")
return nil
}
return UIImage(cgImage: cgimage!)
}

关于这怎么会花这么长时间,有什么建议吗?我可以看到它使用了大约 96% 的 CPU 功率。

func fromData(pair: AllocationPair) -> UIImage? {

let table = pair.table
let data = pair.data

prepareColors(allocations: table.allocations)

let height = data.count
let width = data[0].count

var colors = [PixelData]()

for row in data {
for val in row {

if (val == 0.0) {
colors.append(PixelData(a: 0, r: 0, g: 0, b: 0))
continue
}

if let interval = findInterval(table: table, value: val) {
if let color = intervalColorDict[interval] {
colors.append(PixelData(a: color.a, r: color.r, g: color.g, b: color.b))
}
}
}
}

return imageFromPixels(pixels: colors, width: width, height: height)
}

我已经尝试对它进行时间分析,这是需要时间的输出。

enter image description here

最佳答案

我尝试了您的代码,发现问题不在于您的函数。

我认为您应该使用基于 UInt8 的像素结构,而不是基于 CGFloat 的结构。

我为一个 Cocoa 应用程序翻译了您的代码,结果如下:

public struct PixelData {
var a: UInt8
var r: UInt8
var g: UInt8
var b: UInt8
}

func imageFromPixels(pixels: [PixelData], width: Int, height: Int) -> NSImage? {

let bitsPerComponent = 8
let bitsPerPixel = bitsPerComponent * 4
let bytesPerRow = bitsPerPixel * width / 8

let providerRef = CGDataProvider(
data: NSData(bytes: pixels, length: height * width * 4)
)

let cgimage = CGImage(
width: width,
height: height,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue),
provider: providerRef!,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent
)

if cgimage == nil {
print("CGImage is not supposed to be nil")
return nil
}
return NSImage(cgImage: cgimage!, size: NSSize(width: width, height: height))
}

var img = [PixelData]()

for i: UInt8 in 0 ..< 20 {
for j: UInt8 in 0 ..< 20 {
// Creating a red 20x20 image.
img.append(PixelData(a: 255, r: 255, g: 0, b: 0))
}
}

var ns = imageFromPixels(pixels: img, width: 20, height: 20)

此代码快速且轻便,这是调试系统影响值:

enter image description here

我认为问题出在加载像素数据的部分,检查它并确保它正常工作。

关于ios - 在 Swift 中绘制图像需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353250/

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