如何将带有标签的图像保存到相册?

时间:2016-03-31 00:29:23

标签: ios swift uiimageview

我正在制作相机应用程序,我想为应用程序MSQRD中拍摄的照片添加标签并保存到相册。我有标签显示在图像上,但当我去相册时,它显示的是图像,但没有标签。我的代码出了什么问题。这是我目前使用的代码:

@IBAction func takePicture(sender: AnyObject) {


if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
            videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
            stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
                if (sampleBuffer != nil) {

                    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    let dataProvider = CGDataProviderCreateWithCFData(imageData)
                    let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
                    let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)




        //edited this part it saves the entire view in the photo album except for the image that was taken and the label. 
       UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0)
                    self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
                    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()




                    //saves captured picture to camera roll.
                    UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)





                    //fade in the image that was taken
                    UIView.animateWithDuration(0.5, delay: 0.1, options: UIViewAnimationOptions.CurveLinear, animations: {

                        self.capturedImage.image = image
                        self.capturedImage.alpha = 1.0

                        }, completion: nil)
                        }
                        }
                        }

1 个答案:

答案 0 :(得分:1)

您从capturedImage创建UIGraphicsImageContext,但从未使用过。将UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)更改为UIImageWriteToSavedPhotosAlbum(capturedImage, nil, nil, nil)