私はこの記事に従っています: http://nshipster.com/cmdevicemotion/ CMDeviceMotion の使用方法について。
彼らは記事のこの部分を「Keep It To Yourself」と名付けました。
PromptViewController という識別子を持つビューコントローラーを作成しました。
エラーが表示されます: Use of undeclared type 'PromptViewController' セクション内:
if let promptViewController = self?.storyboard?.instantiateViewControllerWithIdentifier("PromptViewController") as? PromptViewController {`
ここに私の完全なコードがあります:
// ViewController.swift
// upload_images
import UIKit
import MapKit
import CoreLocation
import CoreMotion
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// get magnitude of vector via Pythagorean theorem
func magnitudeFromAttitude(attitude: CMAttitude) -> Double {
return sqrt(pow(attitude.roll, 2) + pow(attitude.yaw, 2) + pow(attitude.pitch, 2))
}
// initial configuration
var initialAttitude = manager.deviceMotion!.attitude
var showingPrompt = false
// trigger values - a gap so there isn't a flicker zone
let showPromptTrigger = 1.0
let showAnswerTrigger = 0.8
if manager.deviceMotionAvailable {
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
[weak self] (data: CMDeviceMotion?, error: NSError?) in
guard let data = data else { return }
// translate the attitude
data.attitude.multiplyByInverseOfAttitude(initialAttitude)
// calculate magnitude of the change from our initial attitude
let magnitude = magnitudeFromAttitude(data.attitude) ?? 0
// show the prompt
if !showingPrompt && magnitude > showPromptTrigger {
if let promptViewController = self?.storyboard?.instantiateViewControllerWithIdentifier("PromptViewController") as? PromptViewController {
showingPrompt = true
promptViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self?.presentViewController(promptViewController, animated: true, completion: nil)
}
}
// hide the prompt
if showingPrompt && magnitude < showAnswerTrigger {
showingPrompt = false
self?.dismissViewControllerAnimated(true, completion: nil)
}
}
}
}
@IBAction func selectPicture(sender: AnyObject) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(ImagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func upload_request(sender: AnyObject) {
UploadRequest()
}
func UploadRequest()
{
let url = NSURL(string: "http://www.kaleidosblog.com/tutorial/upload.php")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (image.image == nil)
{
return
}
let image_data = UIImagePNGRepresentation(image.image!)
if(image_data == nil)
{
return
}
let body = NSMutableData()
let fname = "test.png"
let mimetype = "image/png"
body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"test\"\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("hi\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: \(mimetype)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(image_data!)
body.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
}
task.resume()
}
func generateBoundaryString() -> String
{
return "Boundary-\(NSUUID().UUIDString)"
}
これが私のコードエラーの画像です:
これを修正する方法を知っていますか?
ありがとう!