我有以下代碼拍攝照片並將其保存到相機膠捲。我需要能夠在那裏使用它,然後在保存之後不必回到畫廊並選擇它。保存照片並立即使用它
我還沒有找到任何有關如何做到這一點的例子。
@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);
}
我需要能夠在下面的Web服務功能,使用這個保存的圖像:
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)
}
,當我從庫中選擇圖像,並把我的web服務工作,在我的ImageView上屏幕。但是當我拍攝一張照片時,我需要首先將它保存到圖庫中,但我缺少的是如何以編程方式從庫中引用此新創建的照片。
UPDATE: 我假定從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函數中使用,然後在我的上傳webservice中使用。看來我無法將cameraPath初始化爲零。所以我不知道如何初始化它?
var cameraPath : NSURL = nil
//not allowed
當我剛剛有:
var cameraPath : NSURL
我得到我的控制器沒有initialisers
你可以這樣說:'VAR cameraPath:NSURL ' –