在 didFinishPickingImage 中显示两个图像 - iOS Swift 2.2
Display two images in didFinishPickingImage - iOS Swift 2.2
我有两个 UIImageView
,每个都有两个按钮。一个按钮拍摄一张照片,另一个按钮从图库中选择一张照片。两个按钮都可以正常工作。但是,当我通过第一个 sheet 中的按钮选择图像时,它会显示在两个 UIImageView
中。我的问题是如何才能只在相应的ImageView
?.
中显示图片
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
importedImage.image = image
secondPhoto.image = image
}
// MARK: - Action Sheet
@IBAction func showActionSheet1(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
@IBAction func showActionSheet2(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
发生这种情况是因为您在 ImageView
和 didFinishPickingImage
中都设置了图像,要解决此问题需要保持某种状态,例如单击 Button
以设置 Image
,为此你可以创建一个 Bool
实例并在 Button 的操作中分配它的值,就像这样。
var isFromFirst: Bool = false
@IBAction func showActionSheet1(sender: AnyObject) {
self.isFromFirst = true
...//your other code
}
@IBAction func showActionSheet2(sender: AnyObject) {
self.isFromFirst = false
...//your other code
}
之后像这样检查 didFinishPickingImage
中的布尔值。
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
if (self.isFromFirst) {
importedImage.image = image
}
else {
secondPhoto.image = image
}
}
我有两个 UIImageView
,每个都有两个按钮。一个按钮拍摄一张照片,另一个按钮从图库中选择一张照片。两个按钮都可以正常工作。但是,当我通过第一个 sheet 中的按钮选择图像时,它会显示在两个 UIImageView
中。我的问题是如何才能只在相应的ImageView
?.
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
importedImage.image = image
secondPhoto.image = image
}
// MARK: - Action Sheet
@IBAction func showActionSheet1(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
@IBAction func showActionSheet2(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
发生这种情况是因为您在 ImageView
和 didFinishPickingImage
中都设置了图像,要解决此问题需要保持某种状态,例如单击 Button
以设置 Image
,为此你可以创建一个 Bool
实例并在 Button 的操作中分配它的值,就像这样。
var isFromFirst: Bool = false
@IBAction func showActionSheet1(sender: AnyObject) {
self.isFromFirst = true
...//your other code
}
@IBAction func showActionSheet2(sender: AnyObject) {
self.isFromFirst = false
...//your other code
}
之后像这样检查 didFinishPickingImage
中的布尔值。
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)
if (self.isFromFirst) {
importedImage.image = image
}
else {
secondPhoto.image = image
}
}