【问题标题】:How change/create a new source type for UIImagePicker (SWIFT)如何为 UIImagePicker (SWIFT) 更改/创建新的源类型
【发布时间】:2015-01-30 21:36:16
【问题描述】:

我是 iOS 开发新手,需要帮助。 我使用 UIImagePicker 以便用户选择他的图像。 我希望该用户选择位于应用程序文件夹而不是 iOS 库中的图片。所有这一切都使用 UIImagePicker。这可能吗? 如果我错了,请将我重定向到网站或教程。

谢谢大家。

示例代码:

类视图控制器:UIViewController、UINavigationControllerDelegate、UIImagePickerControllerDelegate {

@IBOutlet var PhotoOpeningView: UIImageView!

let userDefaults = NSUserDefaults.standardUserDefaults()


override func viewDidLoad() {
    super.viewDidLoad()



    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func ChooseAImageBTN(sender: AnyObject) {

    let imagePickerView = UIImagePickerController()
    imagePickerView.delegate = self

    imagePickerView.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
    self.presentViewController(imagePickerView, animated: true, completion: nil)

}


@IBAction func btnSave(sender: UIButton) {
    let LoadSaveNo = PhotoOpeningView
    userDefaults.setObject(LoadSaveNo, forKey: "LoadSave")
    userDefaults.synchronize()
}


func imagePickerController(picker :UIImagePickerController,
    didFinishPickingMediaWithInfo info : [NSObject : AnyObject]) {
        var Image = info[UIImagePickerControllerOriginalImage] as UIImage
        PhotoOpeningView.image = Image
        self.dismissViewControllerAnimated(true, completion: nil)

}

【问题讨论】:

  • 应用程序的文件夹是指文档根目录?就像当您选择按钮选择图像时,它应该显示 UIPopupover 中所有图像的文件夹缩略图。
  • 是文档根目录。我想将图像集成到应用程序中。当用户单击按钮时可以选择图像。但我不想要他 iphone 库中的图像。你说我用 UIPopupover?
  • 好的,你想从应用程序文件夹(意味着来自 NsBundle)访问图像。您可以使用 UIPopoverController 从 NSBundle 加载所有图像。您可以在 UICollectionview 中显示图像,并且您必须创建协议和委托才能将图像获取到您的 viewController。你不能使用苹果 UIImagePickerController。你必须自己做。
  • 好的,非常感谢。我将检查 UIPopoverController 并阅读文档。为了解释,我想创建一个日历。用户为每个输入框(一天中的时间)选择我加入应用程序的文件夹中的 300 张图像之一。所以达到我的目标很好吗?
  • 您可以在 UICollectionViewController 中加载所有这 300 个缩略图大小的图像,并且您可以在 UIPopoverController 中显示 UICollectionViewController(如 iOS 样式)。当您选择照片时,将消息委托给您的 viewController。如果您需要我可以制作的示例代码,请告诉我。

标签: ios xcode swift uikit uiimagepickercontroller


【解决方案1】:
//This class has UIButton when you click on button it opens PhotoPickersVC. Which will show all images from your application
class ViewController: UIViewController,PhotoPickersDelegate {

    @IBOutlet var imageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    @IBAction func buttonBrowsePhotos(sender: UIButton)
    {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let nc :UINavigationController = storyboard.instantiateViewControllerWithIdentifier("PhotoNC") as UINavigationController
        var array:NSArray = nc.viewControllers as NSArray
        let vc:PhotoPickersVC=array.objectAtIndex(0) as PhotoPickersVC;
        vc.delegate=self;
        self.presentViewController(nc, animated: true, completion: nil)

    }

    func selectPhoto(image: UIImage)
    {
        imageView.image=image;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



//Protocol used to send photo to ViewController class using delegate
protocol PhotoPickersDelegate
{
    func selectPhoto(var image : UIImage)
}

//This class shows all the photos of your application
class PhotoPickersVC: UICollectionViewController {
    let reuseIdentifier = "Cell"
    var myImages :NSArray?
    var delegate : PhotoPickersDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        var myBundle : NSBundle = NSBundle.mainBundle();
        myImages = myBundle.pathsForResourcesOfType("jpeg", inDirectory: nil);
    }

    @IBAction func buttonCancel(sender: UIButton)
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return myImages!.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as PhotoCellCollectionViewCell
        print(cell);




        var imageName=myImages?.objectAtIndex(indexPath.row).lastPathComponent
        var imageName1 :NSString=imageName!
        var image:UIImage=UIImage(named: imageName1)!
        cell.imageView.image=image

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {

        if((self.delegate) != nil)
        {
            var imageName=myImages?.objectAtIndex(indexPath.row).lastPathComponent
            var imageName1 :NSString=imageName!
            var image:UIImage=UIImage(named: imageName1)!

            delegate?.selectPhoto(image)
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }


}



//This class used to subclass UICollectionViewCell which is used in PhotoPickersVC
class PhotoCellCollectionViewCell: UICollectionViewCell {
    @IBOutlet var imageView: UIImageView!
}

【讨论】:

  • 谢谢你,我去工作,我给你新的!
【解决方案2】:

查看the UIImagePickerController documentation,我认为您的要求是不可能的。如果您查看 UIImagePickerControllerSourceType,您会发现只有 3 种可能性,PhotoLibrary、SavedPhotosAlbum 或 Camera,这并没有为尝试“自定义”源类型留下很大的空间。

我的建议是使用 UICollectionView 来显示您的图像,并允许您的用户使用集合视图委托方法从中进行选择。如果您只需要一个简单的图像选择器,您就可以很快搞定一个。

目标-C: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

斯威夫特: http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1

【讨论】:

  • 谢谢,但我已经看过文档,我想知道是否可以添加选项。为了更准确,我想创建一个日历。用户在我加入应用程序的文件夹中的 300 张图像中为每个输入框(一天中的时间)选择一张。
  • 文档之外没有秘密。没有办法添加“选项”。我已经构建了一些自定义日历,并且每次我都使用了集合视图。为了向用户展示您的图像并允许他们再次选择,收藏视图是您最好的选择。了解集合视图后,您将很快开始构建出色的应用程序!
  • 是的,我拿了文档,谢谢 Frankie 的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多