gpt4 book ai didi

ios - 在 View 中包含 UIImagePickerController

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:45 25 4
gpt4 key购买 nike

我需要创建一个像页面一样工作的 View works like pages ,带有一个弹出窗口,我可以在其中选择从我的图库中挑选一张照片或创建一张新照片。

目前,我有一个 ViewController,显示为 Popover。在 ViewController 中,我插入了一个 ContainerView 并声明它的类是 UIImageViewController。这是向我展示照片库,但我找不到如何在以下位置选择任何内容:My ViewController presented as Popover .

当我选择一张照片时,没有任何反应。我试过添加一些函数

 (func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!)) 

在我的 ViewController 中,但它不起作用。我读到 UIImagePickerController 不支持子类化,那么页面如何做这些事情?

这是我的 ViewController 代码:

import UIKit

class MenuAddResources: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var newMedia: Bool?
let imagePicker = UIImagePickerController()

@IBAction func takePhoto(sender: AnyObject) {
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
//load the camera interface
let picker : UIImagePickerController = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.delegate = self
picker.allowsEditing = false
self.presentViewController(picker, animated: true, completion: nil)
self.newMedia = true
}
else{
//no camera available
let alert = UIAlertController(title: NSLocalizedString("ERROR", comment: ""), message: NSLocalizedString("NO_CAMERA", comment: ""), preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .Default, handler: {(alertAction)in
alert.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()

}

func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
if error != nil {
let alert = UIAlertController(title: NSLocalizedString("ERROR", comment: ""), message: NSLocalizedString("IMAGE_SAVE_FAILED", comment: ""), preferredStyle: UIAlertControllerStyle.Alert)

let cancelAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .Cancel, handler: nil)

alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})

// Let's store the image
let now:Int = Int(NSDate().timeIntervalSince1970)
let imageData = UIImageJPEGRepresentation(image, 85)
//imageData?.writeToFile(documentsDirectory + "/\(now).jpg", atomically: true)
print(imageData)

/* will do stuff with the image */
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion: nil)
}

}

最佳答案

It appears you are using UIImagePickerViewController through a container view and therefore the delegate isnt set , thus no callbacks to receiving the image picked methods.

要解决此问题,您必须在您的类(class)中覆盖 prepareForSegue并将 segue.destinationViewController 转换到您的选择器并将其 delegate 设置为 self

关于ios - 在 View 中包含 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com