2016-02-15 70 views
3

我創建了兩個應用程序:一個用於mac,一個用於iPhone。 iPhone使用MultipeerConnectivity框架將捕獲的視頻幀發送到Mac。我設法找到代碼用於轉換的UIImage使用此代碼爲灰度:如何在swift中從nsimage創建灰度圖像?

func convertToGrayScale(image: UIImage) -> UIImage { 
     let imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height) 
     let colorSpace = CGColorSpaceCreateDeviceGray() 
     let width = image.size.width 
     let height = image.size.height 
     let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue) 
     let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue) 
     CGContextDrawImage(context, imageRect, image.CGImage) 
     let imageRef = CGBitmapContextCreateImage(context) 
     let newImage = UIImage(CGImage: imageRef!) 
     return newImage 
    } 

在下面的代碼,它發送的視頻幀到Mac:

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) { 
     let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) 
     CVPixelBufferLockBaseAddress(imageBuffer!, kCVPixelBufferLock_ReadOnly) 
     let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!) 
     let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!) 
     let width = CVPixelBufferGetWidth(imageBuffer!) 
     let height = CVPixelBufferGetHeight(imageBuffer!) 
     CVPixelBufferUnlockBaseAddress(imageBuffer!, 0) 
     let colorSpace = CGColorSpaceCreateDeviceRGB() 
     let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue) 
     let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue) 
     let quarzImage = CGBitmapContextCreateImage(context) 
     let image = UIImage(CGImage: quarzImage!) 
     let grayImage = convertToGrayScale(image) 
     let data: NSData = UIImagePNGRepresentation(grayImage)! 
     delegate?.recievedOutput(data) 
    } 

的委託方法是在發送數據使用session.sendData()

所以,這裏來到Mac方面。當MAC收到NSData的,我從數據創建NSImage中,並使用此代碼創建一個PNG圖片文件:

func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) { 
     let image: NSImage = NSImage(data: data)!.imageRotatedByDegreess(270) 
     let cgRef = image.CGImageForProposedRect(nil, context: nil, hints: nil) 
     let representation = NSBitmapImageRep(CGImage: cgRef!) 
     let pngData = representation.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0]) 
     pngData?.writeToFile("/Users/JunhongXu/Desktop/image/\(result.description).png", atomically: true) 
     result[4]++ 
     self.delegate?.presentRecievedImage(image) 
    } 

雖然形象就像下面的圖片,當我打開圖像文件屬性,它是在RGB格式。如何將我的NSImage的ColorSpace更改爲灰度而不是RGB?

enter image description here

+0

我已經刪除了我的答案因爲它沒用。你找到解決方案很酷。如果你願意,不要猶豫,自己發表一個答案。 :) – Moritz

回答

1

我已經找到了一個簡單的解決我的問題。因爲它已經在灰度時transimitted我的Mac,我可以使用下面的代碼,以圖像表達的的ColorSpace轉換爲灰度並將其保存爲.png文件:

let newRep = representation.bitmapImageRepByConvertingToColorSpace(NSColorSpace.genericGrayColorSpace(), renderingIntent: NSColorRenderingIntent.Default) 
let pngData = newRep!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0]) 
pngData?.writeToFile("/Users/JunhongXu/Desktop/image/\(result.description).png", atomically: true)