我正在构建一个应用程序,允许用户将三张图片添加到三个UIImageViews
我添加了三个按钮,例如,当用户点击按钮1时,他应该能够将图像添加到UIImageView
1
但是图像没有来到UIImageView
,我找不到任何解决方案
代码示例:
@IBAction func imageOneBtn(sender: AnyObject) {
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker.delegate = self
picker.allowsEditing = false
self.presentViewController(picker, animated: true, completion: nil)
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageViewOne.image = selectedImage
imageViewOne.contentMode = UIViewContentMode.ScaleAspectFit
picker.dismissViewControllerAnimated(true, completion: nil)
}
}
提前谢谢
答案 0 :(得分:0)
代码似乎是正确的。
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
尝试删除感叹号(!)。
尝试使用imageViewOne.contentMode = .ScaleAspectFit
同时尝试使用var
代替let selectedImage
一般情况下试试此代码
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
var selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage
imageViewOne.contentMode = .ScaleAspectFit
imageViewOne.image = selectedImage
dismissViewControllerAnimated(true, completion: nil)
}
答案 1 :(得分:0)
我已经解决了我的问题,这是任何想要使用它的人的答案
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var oneImg: UIImageView!
@IBOutlet weak var twoImg: UIImageView!
@IBOutlet weak var threeImg: UIImageView!
var num=0;
func config() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
imagePickerController.sourceType = .PhotoLibrary
presentViewController(imagePickerController, animated: true, completion: nil)
}
@IBAction func oneBtn(sender: AnyObject) {
num = 1
config()
}
@IBAction func twoBtn(sender: AnyObject) {
num = 2
config()
}
@IBAction func threeBtn(sender: AnyObject) {
num = 3
config()
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage;
if num == 1 { // switch can be used here
oneImg.image = selectedImage;
} else if (num == 2) {
twoImg.image = selectedImage
} else if (num == 3) {
threeImg.image = selectedImage
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
}