代码:一个错误出来,在FUNC imagePickerController
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var myViewController: UIViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
//ViewControllerのインスタンス化
myViewController = ViewController()
//UINavigationControllerのインスタンス化とrootViewControllerの指定
var myNavigationController = UINavigationController(rootViewController: myViewController!)
//UIWindowのインスタンス化
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
//UIWindowのrootViewControllerにnavigationControllerを指定
self.window?.rootViewController = myNavigationController
//UIWindowの表示
self.window?.makeKeyAndVisible()
return true
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var myImagePicker: UIImagePickerController!
var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Select a Image"
myImageView = UIImageView(frame: self.view.bounds)
// インスタンス生成
myImagePicker = UIImagePickerController()
// デリゲート設定
myImagePicker.delegate = self
// 画像の取得先はフォトライブラリ
myImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
// 画像取得後の編集を不可に
myImagePicker.allowsEditing = false
}
override func viewDidAppear(animated: Bool) {
self.presentViewController(myImagePicker, animated: true, completion: nil)
}
/**
画像が選択された時に呼ばれる.
*/
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
//選択された画像を取得.
var myImage: AnyObject? = info[UIImagePickerControllerOriginalImage]
//選択された画像を表示するViewControllerを生成.
let secondViewController = SecondViewController()
//選択された画像を表示するViewContorllerにセットする.
secondViewController.mySelectedImage = myImage as! UIImage
myImagePicker.pushViewController(secondViewController, animated: true)
}
/**
画像選択がキャンセルされた時に呼ばれる.
*/
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
// モーダルビューを閉じる
self.dismissViewControllerAnimated(true, completion: nil)
}
}
View2.swift
import Foundation
import UIKit
class SecondViewController: UIViewController {
var mySelectedImage: UIImage!
var mySelectedImageView: UIImageView!
override func viewDidLoad() {
self.edgesForExtendedLayout = UIRectEdge.None
self.view.backgroundColor = UIColor.whiteColor()
setImage()
}
/**
選択された画像をUIImageViewにセットする.
*/
func setImage(){
self.title = "Selected Image"
mySelectedImageView = UIImageView(frame: self.view.bounds)
mySelectedImageView.contentMode = UIViewContentMode.ScaleAspectFit
mySelectedImageView.image = mySelectedImage
self.view.addSubview(mySelectedImageView)
}
}
在ViewController.swift,我得到以下错误:
"Objective-C method 'imagePickerController:didFinishPickingMediaWithInfo:'provided by method 'imagePickerController(didFinishPickingMediaWithInfo:)'conflicts with optional requirement method 'imagePickerController(:didFinishPickingMediaWithInfo:)'in protocol 'UIImagePickerControllerDelegate'"
,我应该怎么办???
谢谢!它帮助! – Kaz
@Kaz接受这个答案 –