2017-05-23 4 views
1

나는 다음과 같은 코드가있다 스위프트와 거기에 몇 가지 목표 - C 코드로 변환하고 있습니다에 서명 숯불 만들 :아이폰 OS - 신속한

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]; 

내가 쉽게 색 공간 변환 관리했습니다는하지만 어떻게 만듭니 까를 스위프트 3의 서명되지 않은 문자입니까?

나는 다음을 시도하고 컴파일러는 다음과 같은 오류 메시지를 표시 작동하지 않았다 :

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

을 여기에 코드입니다 :

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)) 

내가 더

을 갈 수 없었다

누군가 도와 줄 수 있습니까?

답변

1

이 시도 감사합니다 ..

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