I am trying to read out some metadata from images which I fetched with the PHPickerViewController.
var config = PHPickerConfiguration()
self.phPicker = PHPickerViewController(configuration: config)
if let picker = self.phPicker {
self.phPicker?.delegate = self
self.present(picker, animated: true) {
self.phPicker = nil
}
}
But the PHPickerResult.assetIdentifier to access those data is always nil.
extension ChatViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true) {
if results.isEmpty {
return
}
assert(results.first!.assetIdentifier != nil) // fails
}
}
}
The trick is the correct configuration of the PHPickerViewController.
var config = PHPickerConfiguration() // WRONG!
let picker = PHPickerViewController(configuration: config)
According to documentation you have to use the PHPhotoLibrary.
A PHPhotoLibrary provides access to the metadata and image data for
the photos, videos and related content in the user's photo library,
including content from the Camera Roll, iCloud Shared, Photo Stream,
imported, and synced from iTunes.
So just use PHPhotoLibrary in the constructor and everything works fine.
var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared()) // CORRECT!
let picker = PHPickerViewController(configuration: config)
I would like to edit/compress video after selection like it was in UIImagePickerController with allowsEditing = true.
Still the new PHPickerViewController doesn't have this property and Apple says that there is no such property anymore here. But there are apps in App Store that pushing "allowsEditing controller" after selecting asset from PHPickerViewController
Here is my PHPickerViewController implementation:
func openImagePicker() {
if #available(iOS 14, *) {
var configuration = PHPickerConfiguration()
configuration.preferredAssetRepresentationMode = .automatic
let picker = PHPickerViewController(configuration: configuration)
// Set the delegate
picker.delegate = self
// Present the picker
present(picker, animated: true)
} else {
// Fallback on earlier versions
imagePicker?.photoGalleryAsscessRequest()
}
}
extension EditController: PHPickerViewControllerDelegate {
#available(iOS 14, *)
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
self.dismiss(animated: true)
}
}
I investigated this issue but indeed there is no support for like as you mention allowsediting.
For example related to your question preset values for video edits was depreciated to UIImagePickerController.ImageURLExportPreset.compatible but There is no support for automatic compression. The picker will always pass the original video/image and it is up to the app to do the necessary compressions or edits.
You can check this Apple Document: imageExportPreset.
Apple specifically mentions that we should be using this new one instead of the older UIImagePickerViewController. If someone wonder more : Meet the new Photos picker
I know there are similar questions available on SO.
The reason why I'm starting a new topic.
I want to take multiple photos and save them into an array for later usage (when user finished taking photos)
I already read the API about showsCameraControl, takePicture() and cameraOverlayView
I managed already to capture multiple photos but I have always to confirm every photo with the "Retake/Use Photo" screen. When tipping on Use Photo, the App saves it into the array.
The same way is already working in the Workflow App from DeskConnect. They have a workflow "Take Photo" where you can choose how many photos you want to take. And they are (in my eyes) using Apple's default controls
Any help is appreciated.
Doesn't matter if it's a solution to avoid "Retake/Use Photo" or to create a custom overly view. For me it is important to keep Apple's default controls so the user feels comfortable with the well known controls.
(I also already took a look on the tutorials Apple provides).
My code so far
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
openCamera()
}
func openCamera() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageArray.append(pickedImage)
print(imageArray.count)
}
self.dismiss(animated: true, completion: nil)
openCamera()
}
This question already has answers here:
Swift ImagePickerController didFinishPickingMediaWithInfo not fired
(2 answers)
Closed 6 years ago.
I'm new to swift...
in fact I'm new to Any Apple products. so basically I'm trying to learn how to build and aircraft while trying to fly and learn how to fly it as well. When this disclaimer is out there I'd like some help ;)
My issue is that I cannot get UIImagePickerController to return with an image to me after I've taken one with the Camera. I think I'm missing something fundamental or at least something important to make it work ;)
But I cannot for the life of me figure out where I'm doing it wrong.
this is what I think is the relevant parts of my ViewController however do ask if you feel I'm leaving something important out:
#IBAction func shootPhoto(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
}
else
{
noCamera()
}
}
private func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image: UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage;
myImageView.contentMode = .scaleToFill
myImageView.image = image
self.dismiss(animated:true, completion: nil)
}
I must confess I do not know what version of swift I'm using. I'm assuming the latest because I just updated the MacBook Pro until it couldn't do any more. I'm using Xcode 8.2.1 and I'm testing this on a IPhone with IOS 10.2.1 I think it's a IPhone 7s.
Any help is appreciated
Edit:
I'm setting that in this function if that is the wrong place I will move it I just cannot figure out to where. ?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view
picker.delegate = self
}
I'm made aware that this might be a duplicate of another question however when I click to that one they are handling the photo library.
I'm trying to handle the camera and I'm having a issue with returning the image to my imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])...
what they talk about doesn't seem to be working for me and they are using things that I've been told is depricated.
edit 2:
this is how I initialise the picker object as well as how the class is defined
class ViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {
let picker = UIImagePickerController()
#IBOutlet weak var myImageView: UIImageView!
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView.image = image
} else if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
myImageView.image = image
}
myImageView.contentMode = .scaleToFill
self.dismiss(animated:true, completion: nil)
}
In iPhone OS 3.0, Apple added the ability to share multiple pictures at once using the "Share" button and selecting multiple images (where a checkmark is used).
I'd love to have a UIImagePickerController which lets the user select multiple images at once, rather than having to go one by one. Is there a way to do this, or do I have to wait until they add this feature?
If you are supporting only iOS 14 and up, you can use Apple's PHPickerViewController. It allows multiple image selection (while UIImagePickerController does not).
An additional benefit to using PHPickerViewController vs other libraries listed above is that the user will not need to grant permission to access your photo library.
Try this wonderful API in swift: ImagePicker. As all other image APIs, it is simple to use and it is very well updated.
1.install pod - pod "BSImagePicker", "~> 2.8"
inside info plist add row Privacy - Photo Library Usage Description
3.paste below code inside a .swift file-
import UIKit
import BSImagePicker
import Photos
class MultipleImgViC: UIViewController {
#IBOutlet weak var imageView: UIImageView!
var SelectedAssets = [PHAsset]()
var photoArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func selectImages(_ sender: Any) {
let vc = BSImagePickerViewController()
self.bs_presentImagePickerController(vc, animated: true, select: { (assest: PHAsset) -> Void in
},
deselect: { (assest: PHAsset) -> Void in
}, cancel: { (assest: [PHAsset]) -> Void in
}, finish: { (assest: [PHAsset]) -> Void in
for i in 0..<assest.count
{
self.SelectedAssets.append(assest[i])
}
self.convertAssetToImages()
}, completion: nil)
}
#IBAction func dismissview(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
extension MultipleImgViC{
func convertAssetToImages() -> Void {
if SelectedAssets.count != 0{
for i in 0..<SelectedAssets.count{
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: SelectedAssets[i], targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: option, resultHandler: {(result,info) -> Void in
thumbnail = result!
})
let data = thumbnail.jpegData(compressionQuality: 0.7)
let newImage = UIImage(data: data!)
self.photoArray.append(newImage! as UIImage)
}
self.imageView.animationImages = self.photoArray
self.imageView.animationDuration = 3.0
self.imageView.startAnimating()
}
}
}
Note :- if pod file give "How to fix “SWIFT_VERSION '3.0' is unsupported, supported versions are: 4.0, 4.2, 5.0” error in Xcode 10.2?
" this error then solve it from this link:- https://stackoverflow.com/a/55901964/8537648
video reference: - https://youtu.be/B1DelPi1L0U
sample image:-
AssetLibrary + UICollectionView ^^
Basically, with StoryBoard, you import aUINavigationController, you change the root controller to anUICollectionViewController (will be your Album list), end add anotherUICollectionViewController (will be your photos list).
Then with Assetlibrary you retrieve user albums and user album content.
I will make a such component as soon as i have some time.
You can use this OpalImagePicker like this (Swift 4):
var imagePicker: OpalImagePickerController!
imagePicker = OpalImagePickerController()
imagePicker.imagePickerDelegate = self
imagePicker.selectionImage = UIImage(named: "aCheckImg")
imagePicker.maximumSelectionsAllowed = 3 // Number of selected images
present(imagePicker, animated: true, completion: nil)
And then implement its delegate:
func imagePickerDidCancel(_ picker: OpalImagePickerController) {
//Cancel action
}
func imagePicker(_ picker: OpalImagePickerController, didFinishPickingImages images: [UIImage]) {
}
No need for a 3rd party library. You can use PHPickerViewController.
https://developer.apple.com/documentation/photokit/phpickerviewcontroller
private func showImagePicker() {
var configuration = PHPickerConfiguration()
configuration.selectionLimit = 5 // Selection limit. Set to 0 for unlimited.
configuration.filter = .images // he types of media that can be get.
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
}
Apple introduced PHPickerViewController in iOS14.
Advantages
No additional permission need to be implemented (its private by default)
Supports Multi-selection (limit can also be specified)
Zooming and previewing the selection
Documentation - https://developer.apple.com/documentation/photokit/phpickerviewcontroller
Video - https://developer.apple.com/videos/play/wwdc2020/10652/
Hope this helps!
How about this way:
Open "photos.app" first, select multiple photos , and copy them ;
In your own app, try to retrieve those copies photos;
I knew that there are some apps did like this, but do not know how can achieve step 2.