0
我目前正在研究一個允許用戶拍攝多個圖像的應用程序,並將它顯示在iCarousel上。我不是很強大,所以我不知道該怎麼做。我的問題是,「如何在用戶拍照後添加並顯示圖像?」我只允許最多3張照片。這裏是我下面的代碼:使用for循環將UIImage附加到數組中
var imageArray : NSMutableArray = NSMutableArray()
@IBOutlet weak var displayView: iCarousel!
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["1.jpg", "2.jpg", "3.jpg"]
displayView.type = iCarouselType.coverFlow2
displayView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var imgView : UIImageView!
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
//let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))
if view == nil {
imgView = UIImageView(frame: CGRect(x: 0, y:0, width: 250, height: 250))
imgView.contentMode = .scaleAspectFit
}
else
{
imgView = view as! UIImageView
}
imgView.image = UIImage(named:"\(imageArray.object(at: index))")
imgView.addGestureRecognizer(tapGesture)
tapGesture.numberOfTapsRequired = 1
imgView.isUserInteractionEnabled = true
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imgView.contentMode = .scaleToFill
imgView.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
return imgView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if (option == .spacing)
{
return value * 1.8
}
return value
}
func numberOfItems(in carousel: iCarousel) -> Int {
return imageArray.count
}
func imageTapped(_ sender: UITapGestureRecognizer)
{
let alert = UIAlertController(title: "Pick Image Source", message: "", preferredStyle: .alert)
let takePhotoBtn = UIAlertAction(title: "Take Photo", style: .default, handler: {(_ action: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
})
let galleryBtn = UIAlertAction(title: "Select from Gallery", style: .default, handler: {(_ action: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
imagePicker.mediaTypes = ["public.image", "public.movie"] //Remove public.movie later
}
})
let cancelBtn = UIAlertAction(title: "Cancel", style: .default, handler: {(_ action: UIAlertAction) -> Void in
// print("Cancel")
})
alert.addAction(takePhotoBtn)
alert.addAction(galleryBtn)
alert.addAction(cancelBtn)
present(alert, animated: true, completion: { _ in })
}