[英]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.