简体   繁体   English

如何设置图像,以便将其保存在Swift 2中

[英]How do I set an image so I can save it in swift 2

Hi I am writing a camera app where the picture will be saved to the cameraroll when you press the photobutton without displaying it. 嗨,我正在编写一个相机应用程序,当您按下照片按钮而不显示它时,照片将被保存到相机胶卷中。 I am almost done, but I have a problem with a piece of code that should work with swift, but don't work with swift 2. 我快完成了,但是我遇到了一段应该与swift一起工作的代码问题,但是不适用于swift 2。

  func didTakePhoto() {
    if let videoConection = stillImageOutput2?.connectionWithMediaType(AVMediaTypeVideo){
        videoConection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput2?.captureStillImageAsynchronouslyFromConnection(videoConection, completionHandler: { (sampleBuffer, ErrorType) -> Void in
            if sampleBuffer != nil {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, .RenderingIntentDefault)

                var savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)

            }
        })
    }
}

@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

When I try to use the image i just created in the function that saves it, it doesn't work. 当我尝试使用刚刚在保存它的函数中创建的图像时,它不起作用。 How can I fix this? 我怎样才能解决这个问题?

Your code is structured like this: 您的代码结构如下:

func didTakePhoto() {
     // ...
     var savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
}
@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

So savedImage is declared locally in, and therefore is entirely confined to, the world of didTakePhoto() . 因此, savedImage是在didTakePhoto()的世界中本地声明的,因此完全局限于该世界。 In the world of takePhotoBtn , no savedImage exists — and thus you get a compiler error. 在世界takePhotoBtn ,没有savedImage存在-这样的话你得到一个编译错误。

You have two choices. 您有两种选择。 You can declare savedImage at a higher level that both methods can see: 您可以在两个方法都可以看到的更高级别上声明savedImage

var savedImage:UIImage!
func didTakePhoto() {
     // ...
     savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
}
@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

Or, you can have didTakePhoto return savedImage as its result: 或者,您可以使didTakePhoto 返回 savedImage作为其结果:

func didTakePhoto() -> UIImage {
     // ...
     let savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
     return savedImage
}
@IBAction func takePhotoBtn(sender: UIButton) {
    UIImageWriteToSavedPhotosAlbum(didTakePhoto(), nil, nil, nil)
}

I'm using this snippet in my project: 我在我的项目中使用以下代码段:

func saveImage(image: UIImage) 
   let destinationPath = documentsPath.stringByAppendingPathComponent("test.png")
   UIImagePNGRepresentation(rotateImage(image))!.writeToFile(destinationPath, atomically: true)

 }


func rotateImage(image: UIImage) -> UIImage {
    print(image.imageOrientation.hashValue )
    if (image.imageOrientation == UIImageOrientation.Up ) {

        return image //UIImage(CGImage: image, scale: 0.5, orientation: UIImageOrientation.Up)
    }

    UIGraphicsBeginImageContext(image.size)

    image.drawInRect(CGRect(origin: CGPoint.zero, size: image.size))
    let copy = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()
    return copy
}

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

相关问题 如何设置成功/失败块,以便可以使用Swift将字符串传递给它? - How do I set up a success/failure block so that I can pass a string into it using Swift? 如何将图像保存在核心数据中然后进行检索? 使用迅捷 - How do i save an image in core data then retrieve it? Using swift 如何快速保存用户选择的图像? - How do i save an image that was selected by a user with swift? 如何快速保存和获取userDefaults中的图像值 - how i can save and fetch the image value in userDefaults in swift 如何在swift中设置表格单元格的背景图像大小? - How do I set the background image size of a table cell in swift? 如何使用Swift在iOS中使用透明设置背景图片? - How do I set a background image in iOS with transparency using Swift? iOS,Swift:如何保存包含自定义对象的数组,以便可以加载以进行重用? - iOS, Swift: How to save an array that contains custom objects so that I can be loaded for reuse? Xcode 8 / Swift 3-如何将按钮约束锁定在缩小位置以缩放图像,以便它们随图像放大和缩小? - Xcode 8/ Swift 3 - How can I lock button constraints to a pinch to zoom image so they zoom in and out with the image? 如何在Swift中保存一次整数? - How do I save an integer once in Swift? 如何保存快速数组? - How do I save a swift array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM