2017-05-23 3 views
1

ich einige Objective-C-Code Swift Umwandlung und darin gibt es den folgenden Code ein:iOS - erstellen unsigned char in schnellen

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char rgba[4]; 
CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 

CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage); 
CGColorSpaceRelease(colorSpace); 
CGContextRelease(context); 


UIColor *averageColor = [UIColor colorWithRed:((CGFloat)rgba[0])/255.0 green:((CGFloat)rgba[1])/255.0 blue:((CGFloat)rgba[2])/255.0 alpha:alpha]; 
averageColor = [averageColor colorWithMinimumSaturation:0.15]; 

Ich habe es geschafft, den Farb leicht zu konvertieren, aber wie kann ich erstellen nicht signierte Zeichen in Swift 3?

habe ich versucht, die folgende und es hat der Compiler veranlassen, die folgenden Fehler nicht:

Cannot convert value of type '[CUnsignedChar]' to expected argument type 'UnsafeMutableRawPointer?'

und hier ist der Code:

let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB() 
let rgba = [CUnsignedChar](repeating: 0,count: 4) 
// error on this line because of the rgba  
let context: CGContext = CGContext.init(data: rgba, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast | CGBitmapInfo.byteOrder32Big) 


context.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: 1, height: 1)) 

ich nicht weiter gehen konnte

Kann jemand helfen?

danke

Antwort

1

die Sie interessieren ..

let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB() 
let data = UnsafeMutableRawPointer.allocate(bytes: 4, alignedTo: 1) // 4 unsigned char = 4 bytes 
let context: CGContext = CGContext.init(data: data, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast | CGBitmapInfo.byteOrder32Big) 
let rgba = Array(UnsafeBufferPointer(start: data.assumingMemoryBound(to: UInt8.self), count: 4)) 
// .... other