将UIimage(拍照)转换为NSData [英] Convert UIimage (take a picture) to NSData

查看:444
本文介绍了将UIimage(拍照)转换为NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这种方法拍照。

func convertImageFromCMSampleBufferRef(sampleBuffer:CMSampleBuffer) -> CIImage{
    let pixelBuffer:CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!;
    let ciImage:CIImage = CIImage(CVPixelBuffer: pixelBuffer)

    if done == true {

        newImage =  UIImage(CIImage:ciImage, scale: CGFloat(1.0), orientation: .DownMirrored)

        var imageData = UIImageJPEGRepresentation(newImage, 0.6)
        var compressedJPGImage = UIImage(data: imageData)
        UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)       
    }

    return ciImage;
}

代码应该有效,但变量 imageData = nil
我尝试在PNG中转换图像,但结果相同。

The code should work, but the variable imageData = nil I tried converting the image in PNG, but with the same result.

with print

with print


newImage =,{720,1280} imageData = nil

newImage = , {720, 1280} imageData = nil


推荐答案

你必须将CIImage转换为CGImage,然后将CGImage转换为UIImage,然后将UIImage转换为NSData。

You must convert CIImage to a CGImage, then CGImage to an UIImage, and then UIImage to NSData.

static let context = CIContext(options:nil);
let tempImage:CGImageRef = context.createCGImage(ciImage, fromRect: ciImage.extent())
let image = UIImage(CGImage: tempImage);
let imageData: NSData? = UIImageJPEGRepresentation(image, 0.6);

这篇关于将UIimage(拍照)转换为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆