How can I make this imagePickerController able to accept edited images? - swift

How can I make this imagePickerController able to accept edited images?
When selecting an image and adjusting it (zoom in and out) and selecting done, the image reverts back to its original state. What can I add to to my code to have the zoomed image save and show up in my image view?
The following is my code so far:
let cameraRollAction = UIAlertAction(title: "Camera Roll", style: .default) { (action) in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.mediaTypes = [kUTTypeImage as String]
picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(picker, animated: true, completion: nil)
self.newPic = false
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqual(to: kUTTypeImage as String) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
profileImageView.image = selectedImage
if newPic == true {
UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(imageError), nil)
}
}
self.dismiss(animated: true, completion: nil)
}

Instead of
UIImagePickerControllerOriginalImage
use
UIImagePickerControllerEditedImage

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqual(to: kUTTypeImage as String) {
let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage ?? info[UIImagePickerControllerOriginalImage] as! UIImage // will return original image if edited image is not available
profileImageView.image = selectedImage
if newPic == true {
UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(imageError), nil)
}
}
self.dismiss(animated: true, completion: nil)
}

Related

All sounds in the app are muted after using the image picker

All sounds in the app are muted after using the image picker
There is no separate volume setting when loading the image picker.
Why is this?
my code
#IBAction func tapRecording(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.mediaTypes = [kUTTypeMovie as String]
imagePicker.videoMaximumDuration = 120 // or whatever you want
imagePicker.videoQuality = .typeMedium
imagePicker.allowsEditing = false
player.pause()
playButton.isHidden = false
exampleLabel.isHidden = false
self.navigationController?.present(imagePicker, animated: true)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
let videoURL = info[.mediaURL] as! URL
let vc = UIStoryboard(name: "Growth", bundle: nil).instantiateViewController(identifier: "GrowthSaveViewController") as? GrowthSaveViewController
vc!.videoUrl = videoURL
vc?.videoData = videoData
self.navigationController?.pushViewController(vc!, animated: true)
}

How to extract image filename from UIImage

I have <UIImage: 0x6040000b9800> , {1668, 2500} name for taken photo from gallery. But I need imagename in String. Anyone help me to solve in swift 4? I have given codes below:
extension PhotoViewController: ImagePickerDelegate {
func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
}
func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
self.image = images[0]
let navigati:PhotoDescriptorController = storyboard!.instantiateViewController(withIdentifier: StoryBoard.PhotoDescriptorController) as! PhotoDescriptorController
navigati.imageDest = self.image
self.navigationController?.pushViewController(navigati, animated: true)
imagePicker.dismiss(animated: true, completion: nil)
}
func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
imagePicker.dismiss(animated: true, completion: nil)
}
Have you tried using Photo framework to get imageName?
See this example.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let picURL = info[UIImagePickerControllerReferenceURL] as? URL {
let imgData = PHAsset.fetchAssets(withALAssetURLs: [picURL], options: nil)
let asset = imgData.firstObject
print(asset?.value(forKey: "filename"))
}
dismiss(animated: true, completion: nil)
}

Error creating an image format with an unknown type

While choosing an image from the image picker in iOS 10, I am getting an error - Creating an image format with an unknown type is an error
Here is my code:
func CamaraInit(){
let isOk = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
if isOk {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.cameraDevice = .rear
imagePicker.showsCameraControls = true
imagePicker.allowsEditing = true
imagePicker.cameraCaptureMode = .photo
// imagePicker.cameraCaptureMode = .video
self.present(imagePicker, animated: true, completion: nil)
}
}
func imageInit(){
let isOk = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary)
if isOk {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
//imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var useImage : UIImage!
let originalImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage
if editedImage == nil {
useImage = originalImage
} else {
useImage = editedImage
}
if picker.sourceType == UIImagePickerControllerSourceType.camera {
UIImageWriteToSavedPhotosAlbum(useImage, nil, nil, nil)
}
self.UIPhotoImage = useImage
self.viewChange()
picker.dismiss(animated: true, completion: nil)
}
I have searched a lot of materials but still have no idea why.
seems it is kind of warning not error,at least in my case...after uploading your image,viewWillApear will be automatically called again.
thats why you think image was not uploaded.

Where is the image captured from iOS?

When using the default camera in swift, I use this to take the picture and then show it in an imageView:
#IBAction func takePhoto() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
self.presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)
}
But I want to take the image that I just took and store that image in a constant, but I don't know what is the image I took. I have tried using this as the image
UIImage(named: UIImagePickerControllerOriginalImage)
UIImage(named: "spect a string here") and UIImagePickerControllerOriginalImage returns me a string so that is why I thought that could be the image, but it seems that it won't work.
You can say something like:
var photo = UIImage() // Accessible from anywhere in the class
#IBAction func takePhoto() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
self.presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
photo = info[UIImagePickerControllerOriginalImage] as? UIImage // Assign `photo` to the image
}
In the above code, photo is a variable accessible from anywhere in the class. Therefore, it is accessible from all of your functions and handlers. info[UIImagePickerControllerOriginalImage] as? UIImage is the image from the camera, and in the code, you're changing the variable photo to the image from the camera. Now, the image from the camera is available later from anywhere in that class.

How to convert image to binary in Swift

I created an app to take an image and convert this image to binary and send to server. I take the image but I can't convert it.
I use this code :
func cameraa(){
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage;dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func Encode(sender: UIButton) {
var imageEncode = ImageDisplay.image
let image : UIImage = UIImage(imageEncode)
let imageData = UIImagePNGRepresentation(image)
print(imageData)
My error in parse image(imageEncode) to (let image : UIImage = UIImage(imageEncode))
The ImageDisplay.image is already a UIImage. So you needn't to convert it to UIImage again. Just do that:
let imageData = UIImagePNGRepresentation(ImageDisplay.image)
print(imageData)