我一直在试图弄清楚如何将一组rgb像素数据转换为Swift中的UIImage.
我将每个像素的rgb数据保存在一个简单的结构中:
public struct PixelData {
var a: Int
var r: Int
var g: Int
var b: Int
}
Run Code Online (Sandbox Code Playgroud)
我已经开始使用以下函数,但生成的图像不正确:
func imageFromARGB32Bitmap(pixels:[PixelData], width: Int, height: Int)-> UIImage {
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue)
let bitsPerComponent:Int = 8
let bitsPerPixel:Int = 32
assert(pixels.count == Int(width * height))
var data = pixels // Copy to mutable []
let providerRef = CGDataProviderCreateWithCFData(
NSData(bytes: &data, length: data.count * sizeof(PixelData))
)
let cgim = CGImageCreate(
width,
height,
bitsPerComponent,
bitsPerPixel,
width * Int(sizeof(PixelData)), …Run Code Online (Sandbox Code Playgroud) 当我构建并部署到iPhone时,就可以正常通话UIApplication.shared.setAlternateIconName了。在iPad Pro上运行时,出现以下错误:
错误域= NSCocoaErrorDomain代码= 4“该文件不存在。” UserInfo = {NSUnderlyingError = 0x1c0857700 {Error Domain = LSApplicationWorkspaceErrorDomain代码= -105“在CFBundleAlternateIcons条目中找不到图标名” UserInfo = {NSLocalizedDescription =在CFBundleAlternateIcons条目中找不到图标名}}}
使用以下代码:
UIApplication.shared.setAlternateIconName(icons[indexPath.row].name) { err in
if let err = err {
print("Woops ! \(String(describing: err))")
}
}
Run Code Online (Sandbox Code Playgroud)
我有每个图标的标准,2x和3x版本,范围从60x60、120x120和180x180。这些图像松散地放置在项目中,而不是资产束中。它们在我的Info.plist中引用。
到底是怎么回事?为什么iPhone和iPad有区别?