保存照片并立即使用
Saving a photo and using it immediately
我有以下用于拍照并将其保存到相机胶卷的代码。我需要能够在那里使用它,然后在保存后无需返回画廊并选择它。
我还没有找到任何关于如何执行此操作的示例。
@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);
}
我需要能够在以下网络服务功能中使用这个保存的图像:
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 中时,我的网络服务正在运行。但是当我拍照时,我需要先将它保存到我现在已经完成的库中,但我缺少的部分是如何以编程方式从库中引用这张新创建的照片。
更新:
我假设从 nsurl 到 nsdata 的转换在下面的从相机块中检索如下。请注意,当用户选择拍照选项时,cameraBool 设置为 true。 ImageChanged在imagePickerController函数中设置
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 函数中使用,然后在我的上传 web 服务中使用。看来我无法将 cameraPath 初始化为 nil。所以我不确定如何初始化它?
var cameraPath : NSURL = nil
//not allowed
当我只有:
var cameraPath : NSURL
我收到的错误是我的控制器没有初始化程序
您可以为此使用 protocols/Delegation-pattern。见 Apple Doc.
基本上,您在 class 中使用所需的函数创建一个协议,设置一个委托变量,然后将您的图像移交给新的视图控制器。你可以找到教程 here.
使用 ALAssetsLibrary,如果您使用 UIImageWriteToSavedPhotosAlbum 保存图像,则不会获得指向它的指针。更多 info.
编辑:
我认为@DharmeshKheni 的解决方案应该有效。我的猜测是您在设置之前阅读了 cameraPath。换句话说:我认为你在保存之前尝试使用新图片。你能说出 cameraPath 的设置位置和读取位置吗?
您可以通过以下方式获取相机最近添加的图像的路径:
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
我有以下用于拍照并将其保存到相机胶卷的代码。我需要能够在那里使用它,然后在保存后无需返回画廊并选择它。
我还没有找到任何关于如何执行此操作的示例。
@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);
}
我需要能够在以下网络服务功能中使用这个保存的图像:
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 中时,我的网络服务正在运行。但是当我拍照时,我需要先将它保存到我现在已经完成的库中,但我缺少的部分是如何以编程方式从库中引用这张新创建的照片。
更新: 我假设从 nsurl 到 nsdata 的转换在下面的从相机块中检索如下。请注意,当用户选择拍照选项时,cameraBool 设置为 true。 ImageChanged在imagePickerController函数中设置
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 函数中使用,然后在我的上传 web 服务中使用。看来我无法将 cameraPath 初始化为 nil。所以我不确定如何初始化它?
var cameraPath : NSURL = nil
//not allowed
当我只有:
var cameraPath : NSURL
我收到的错误是我的控制器没有初始化程序
您可以为此使用 protocols/Delegation-pattern。见 Apple Doc.
基本上,您在 class 中使用所需的函数创建一个协议,设置一个委托变量,然后将您的图像移交给新的视图控制器。你可以找到教程 here.
使用 ALAssetsLibrary,如果您使用 UIImageWriteToSavedPhotosAlbum 保存图像,则不会获得指向它的指针。更多 info.
编辑: 我认为@DharmeshKheni 的解决方案应该有效。我的猜测是您在设置之前阅读了 cameraPath。换句话说:我认为你在保存之前尝试使用新图片。你能说出 cameraPath 的设置位置和读取位置吗?
您可以通过以下方式获取相机最近添加的图像的路径:
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