如何 change/create UIImagePicker 的新源类型 (SWIFT)
How change/create a new source type for UIImagePicker (SWIFT)
我是 iOS 开发的新手,我需要帮助。
我使用 UIImagePicker 以便用户选择他的图像。
我希望用户选择位于应用程序文件夹中的图片,而不是 iOS 库中的图片。所有这些都使用 UIImagePicker。这可能吗?
如果我错了,请将我重定向到网站或教程。
谢谢大家
示例代码:
class ViewController: 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)
}
通过 the UIImagePickerController documentation 浏览了一下,我不认为你问的是可能的。如果您看一下 UIImagePickerControllerSourceType,您会发现只有 3 种可能性,PhotoLibrary、SavedPhotosAlbum 或 Camera,这并没有为尝试 'custom'.
我的建议是使用 UICollectionView 来显示您的图像,并允许您的用户使用集合视图委托方法从中进行选择。如果您只需要一个简单的图像选择器,您就可以很快搞定。
Objective-C:
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
Swift:
http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-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!
}
我是 iOS 开发的新手,我需要帮助。 我使用 UIImagePicker 以便用户选择他的图像。 我希望用户选择位于应用程序文件夹中的图片,而不是 iOS 库中的图片。所有这些都使用 UIImagePicker。这可能吗? 如果我错了,请将我重定向到网站或教程。
谢谢大家
示例代码:
class ViewController: 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)
}
通过 the UIImagePickerController documentation 浏览了一下,我不认为你问的是可能的。如果您看一下 UIImagePickerControllerSourceType,您会发现只有 3 种可能性,PhotoLibrary、SavedPhotosAlbum 或 Camera,这并没有为尝试 'custom'.
我的建议是使用 UICollectionView 来显示您的图像,并允许您的用户使用集合视图委托方法从中进行选择。如果您只需要一个简单的图像选择器,您就可以很快搞定。
Objective-C: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
Swift: http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-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!
}