保存照片并立即使用 [英] Saving a photo and using it immediately
问题描述
我有以下代码用于拍摄照片并将其保存到相机胶卷。我需要能够使用它,然后保存后,不必回到画廊和选择它。
I have the following code for taking a photo and saving it to the camera roll. I need to be able to use it there and then after saving without having to go back into the gallery and selecting it.
我没有找到任何例子,如何这样做。
I haven't found any examples on how to do this.
@IBOutlet weak var imagePicked: UIImageView!
@IBAction func CameraBtn(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
cameraBool = true
}
}
@IBAction func GalleryBtn(sender: UIButton) {
println("gallery button clicked")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
cameraBool = false
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
imagePicked.image = image
if (cameraBool){
var imageData = UIImageJPEGRepresentation(imagePicked.image, 0.6)
var compressedJPGImage = UIImage(data: imageData)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage, nil, nil, nil)
}
self.dismissViewControllerAnimated(true, completion: nil);
}
我需要能够在以下webservice函数中使用此保存的图像:
I need to be able to use this saved image in the following webservice function:
func urlRequestWithComponents(urlString:String, parameters:Dictionary<String, String>, imageData:NSData) -> (URLRequestConvertible, NSData) {
// create url request to send
var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)
mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
let boundaryConstant = "myRandomBoundary12345";
let contentType = "multipart/form-data;boundary="+boundaryConstant
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
// create upload data to send
let uploadData = NSMutableData()
// add image
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Disposition: form-data; name=\"file\"; filename=\"file.png\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData(imageData)
// add parameters
for (key, value) in parameters {
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
}
uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
// return URLRequestConvertible and NSData
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
}
当我从图库中选择图片并将其放在我的imageView屏幕上时,我的网络服务正在工作,但是当我拍照时,我需要将其保存到图书馆
My web service is working when I choose an image from the gallery and place that in my imageView on screen. But when I take a photo, I need to save it to the library first which I have now done but the piece I am missing is how to reference this newly created photo from the library programmatically.
UPDATE: $ b,我已经做了,但我缺少的是如何从程序库中引用这个新创建的照片。 $ b假设从nsurl到nsdata的转换如下在从下面的摄像机块中检索:注意当用户选择拍摄图片时,cameraBool设置为true ImageChanged在imagePickerController函数中设置
UPDATE: I assume converting from nsurl to nsdata would be as below in the retrieved from camera block below. Note cameraBool is set to true when the user selects the option to take a picture. ImageChanged is set in the imagePickerController function
if (imageChanged){
//changed from placeholder
if (!cameraBool){
//retrieved from gallery
let image = pic
let imageData = UIImagePNGRepresentation(image)
}
else{
//retrieved from camera
let imageData = NSData(contentsOfURL: cameraPath)
}
}
else{
//upload nil
let imageData = nil
}
我的问题是图像的路径只在我的imagePickerController函数中定义。所以我需要声明cameraPath在文件的顶部,所以它可以在imagePickerController函数,然后在我的上传webservice中使用。它似乎我不能初始化cameraPath为零。所以我不知道如何初始化它。
My issue is that the path of the image is only defined in my imagePickerController function. So I need to declare the cameraPath at the top of the file so it can be used in the imagePickerController function and then in my upload webservice. It seems I cannot initialise cameraPath as nil. So I'm not sure how to initialise it?
var cameraPath : NSURL = nil
//not allowed
当我只有:
var cameraPath : NSURL
我收到我的控制器没有的错误初始化
I am getting the error that my controller has no initialisers
推荐答案
您可以通过相机获得最近添加的图片的路径:
You can get Path of the recently added image by camera this way:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
imagePicked.image = image
if (cameraBool){
var imageData = UIImageJPEGRepresentation(imagePicked.image, 0.6)
var compressedJPGImage = UIImage(data: imageData)
ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedJPGImage!.CGImage, orientation: ALAssetOrientation(rawValue: compressedJPGImage!.imageOrientation.rawValue)!,
completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
println("\(path)") //Here you will get your path
})
}
self.dismissViewControllerAnimated(true, completion: nil);
}
这将打印如下路径:
assets-library://asset/asset.JPG?id=C5E0EB97-5D3D-41F9-8782-F48140144432&ext=JPG
这篇关于保存照片并立即使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!