ios – 在Swift中绘制图像永远

前端之家收集整理的这篇文章主要介绍了ios – 在Swift中绘制图像永远前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我创建一个映像映射漂移到像素值,并将其用作Google地图上的叠加层,但它永远要做,Android中的同样的事情几乎是即时的.我的代码如下所示:
  1. private func imageFromPixels(pixels: [PixelData],width: Int,height: Int) -> UIImage? {
  2.  
  3. let bitsPerComponent = 8
  4. let bitsPerPixel = bitsPerComponent * 4
  5. let bytesPerRow = bitsPerPixel * width / 8
  6.  
  7. let providerRef = CGDataProvider(
  8. data: NSData(bytes: pixels,length: height * width * 4)
  9. )
  10.  
  11. let cgimage = CGImage(
  12. 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
  13. )
  14.  
  15. if cgimage == nil {
  16. print("CGImage is not supposed to be nil")
  17. return nil
  18. }
  19. return UIImage(cgImage: cgimage!)
  20. }

关于这个可能需要这么长时间的任何建议?我可以看到它使用大约96%的cpu电源.

  1. func fromData(pair: AllocationPair) -> UIImage? {
  2.  
  3. let table = pair.table
  4. let data = pair.data
  5.  
  6. prepareColors(allocations: table.allocations)
  7.  
  8. let height = data.count
  9. let width = data[0].count
  10.  
  11. var colors = [PixelData]()
  12.  
  13. for row in data {
  14. for val in row {
  15.  
  16. if (val == 0.0) {
  17. colors.append(PixelData(a: 0,r: 0,g: 0,b: 0))
  18. continue
  19. }
  20.  
  21. if let interval = findInterval(table: table,value: val) {
  22. if let color = intervalColorDict[interval] {
  23. colors.append(PixelData(a: color.a,r: color.r,g: color.g,b: color.b))
  24. }
  25. }
  26. }
  27. }
  28.  
  29. return imageFromPixels(pixels: colors,width: width,height: height)
  30. }

我试图时间配置它,这是输出需要时间.

解决方法

我尝试了你的代码,我发现问题不在于你的功能.

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

我翻译了可可应用程序的代码,结果是:

  1. public struct PixelData {
  2. var a: UInt8
  3. var r: UInt8
  4. var g: UInt8
  5. var b: UInt8
  6. }
  7.  
  8. func imageFromPixels(pixels: [PixelData],height: Int) -> NSImage? {
  9.  
  10. let bitsPerComponent = 8
  11. let bitsPerPixel = bitsPerComponent * 4
  12. let bytesPerRow = bitsPerPixel * width / 8
  13.  
  14. let providerRef = CGDataProvider(
  15. data: NSData(bytes: pixels,length: height * width * 4)
  16. )
  17.  
  18. let cgimage = CGImage(
  19. width: width,intent: .defaultIntent
  20. )
  21.  
  22. if cgimage == nil {
  23. print("CGImage is not supposed to be nil")
  24. return nil
  25. }
  26. return NSImage(cgImage: cgimage!,size: NSSize(width: width,height: height))
  27. }
  28.  
  29. var img = [PixelData]()
  30.  
  31. for i: UInt8 in 0 ..< 20 {
  32. for j: UInt8 in 0 ..< 20 {
  33. // Creating a red 20x20 image.
  34. img.append(PixelData(a: 255,r: 255,b: 0))
  35. }
  36. }
  37.  
  38. var ns = imageFromPixels(pixels: img,width: 20,height: 20)

这个代码快速和轻便的,这里是调试系统的影响值:

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

猜你在找的iOS相关文章