我目前正致力于一个允许用户拍摄多张图片的应用程序,并将其显示在iCarousel上。我不是很强壮,所以我不知道该怎么做。我的问题是,"如何在用户拍照后附加和显示图像?"我只允许最多3张照片。以下是我的代码:
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 })
}
答案 0 :(得分:0)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if imageArray.count == 3{
return //you can jump to Show the Carousel here
}
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imgView.contentMode = .scaleToFill
imgView.image = pickedImage
// Add image to Array Here
imageArray.add(image)
}
// you can also show refresh carousel from here so new image is availabe in carousel
picker.dismiss(animated: true, completion: nil)
}
如果您允许最多3张图像,那么您也可以在imageTapped
函数中写入检查条件。