有一段时间,在场景上工作,允许用户输入分机号码myExtension
和说明myText
,然后点击拍照(takePhoto
) 。接下来打开相机,用户拍照,将图像加载到图像查看器中,然后有一个保存按钮saveImage
,将图像上传到Web服务器。这一切都很好......但是为了省去不好名的图像的麻烦,我想禁用所有字段和按钮,直到输入文本。
例如:扩展解锁说明,说明解锁TAKE PHOTO BUTTON,并且在图像视图不为空之前不会激活保存。并且可能隐藏图像视图直到拍摄照片。
我需要用户拍摄多张照片,所以我希望文字保留在字段中,但在我拍照后清除图像视图并隐藏,让他们再次点按拍照。
注意:我将日期时间附加到文件名,以便它不会覆盖文件。
附件是控制器代码。任何帮助表示赞赏。我尝试了一些选项,但似乎无法让它正常运行。
import UIKit
class PhotoViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var myExtension: UITextField!
@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var myText: UITextField!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var takePhoto: UIButton!
@IBOutlet weak var saveImage: UIButton!
@IBAction func uploadButtonTapped(sender: AnyObject) {
myImageUploadRequest()
}
@IBAction func selectPhotoButtonTapped(sender: AnyObject) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//myImageUploadRequest()
}
func myImageUploadRequest()
{
let myUrl = NSURL(string: "https://website.com/api/upload1.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let param = [
"myText" : myText.text!,
"myExtension" : myExtension.text!
]
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(myImageView.image!, 1)
if(imageData==nil) { return; }
request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary)
myActivityIndicator.startAnimating();
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("****** response data = \(responseString!)")
//Update
// var err: NSError?
// var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print(jsonResult) }
} catch let error as NSError {
print(error.localizedDescription)
}
dispatch_async(dispatch_get_main_queue(),{
self.myActivityIndicator.stopAnimating()
self.myImageView.image = nil;
});
}
task.resume()
}
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
var now: String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMDDyyyy"
return dateFormatter.stringFromDate(NSDate())
}
var time: String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HHmmss"
return dateFormatter.stringFromDate(NSDate())
}
let filename = (myText.text! + "_" + now + time + ".jpg")
let mimetype = "image/jpg"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendData(imageDataKey)
body.appendString("\r\n")
body.appendString("--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}
}
extension NSMutableData {
func appendString(string: String) {
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
appendData(data!)
}
}
答案 0 :(得分:0)
经过多次试验和错误,我得到了这个工作。
基本上我创建了一个完成所有检查的功能。
感谢...