简体   繁体   English

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

[英]How do I save an image with a label on it to the photo album?

Im making a camera app and I want to add a label to the pictures that are taken like in the app MSQRD and save to the photo album. 我正在制作一个相机应用程序,我想为在应用程序MSQRD中拍摄的照片添加标签,然后保存到相册中。 I got the label to display on to the image but when I go to the photo album it shows an image but without the label. 我得到了要显示在图像上的标签,但是当我进入相册时,它显示的是图像但没有标签。 What am I doing wrong with my code. 我的代码在做什么错。 Here is the code that Im using currently: 这是我当前使用的代码:

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

You made the capturedImage from UIGraphicsImageContext , but never used. 您是从UIGraphicsImageContext capturedImage ,但从未使用过。 Change UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) to UIImageWriteToSavedPhotosAlbum(capturedImage, nil, nil, nil) . UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)更改为UIImageWriteToSavedPhotosAlbum(capturedImage, nil, nil, nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM