This question already has an answer here:
PDFKit is vertically flipping PDFPage initialised with image
(1 answer)
Closed 5 years ago.
When I use the camera to take a picture in my app the images are then displayed upside down.
I use a UIImagePickerController to take a photo and just select the one i am happy with. Upon selection, the following function is execute:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("image selected ...")
// Get picked image info dictionary
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
// Image is upside down. Need to re-orient
// Add captured and selected image to your Photo Library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
// Create PDFDocument object and display in PDFView
let document = createPDFDataFromImage(image: image)
myPDFView.document = document
// Dimiss imagePicker
dismiss(animated: true, completion: nil)
}
func createPDFDataFromImage(image: UIImage) -> PDFDocument{
let document = PDFDocument.init()
let imagePDF = PDFPage.init(image: image)
document.insert(imagePDF!, at: 0)
return document
}
How can I stop the pictures from being upside down?
Here is one way to do it:
var flippedImage: UIImage?
if let origImage = UIImage(named: "s1") {
if let cg = origImage.cgImage {
flippedImage = UIImage(cgImage: cg, scale: 1.0, orientation: UIImageOrientation.downMirrored)
}
}
theImageView.image = flippedImage
Related
Hi I am new to pdfkit and iOS applications, I want to take a picture with device and then convert the image to pdf. taking pictures with camera and saving it on Camera Roll works completely fine, the problem is when I want to create pdf from that image it needs the name of the image, how I can find it?
and another question is it possible to implement any pdf editor with pdfkit to edit image?
Appreciate any help. below is my imagePickerController codes.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
if (Mylibrary)
{
}
else
{
UIImageWriteToSavedPhotosAlbum(pickedImage!, nil, nil, nil);
let image = UIImage(named: "????") //the problem is here
let newPage = PDFPage(image: image!)
}
picker.dismiss(animated: true, completion: nil)
}
There's no need to instantiate a new image since the image is already stored in the pickedImage variable. You just need to initialize the new PDFPage object with pickedImage. Use ImageKit to edit the image before using it to initialize a new PDFPage object.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
// Adds pickedImage to the user’s Camera Roll album.
UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)
// Creates a new PDFPage object and initializes it with pickedImage.
let newPage = PDFPage(image: pickedImage)
dismiss(animated: true, completion: nil)
}
I think I have a similar problem like this other question's title suggests, but the accepted answer is not at all what I need.
I want to "force" the user to use a square image. Currently, when choosing a horizontally "longer" image (landscape image), one could theoretically make a selection with black bars top and bottom which results in a selected image that is NOT squared.
Here my code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selected: UIImage?
if let img = info[UIImagePickerControllerEditedImage] as? UIImage {
selected = img
selected = UIImage.imgToSquare(image: selected)
doSomethingWithImage(selected!)
}
picker.dismiss(animated: true, completion: nil)
}
UIImage.imgToSquare(image: UIImage) is an Extension that I have and use as a temporary fallback but it would be much more elegant if the "maximum pinch/zoom-out" gives a square image.
To whoever marked this as a duplicate.. In which way is this a duplicate? I could not find a solution on this on the whole internet, and what you linked was not related to UIImagePickerController....
I already said I'm able to crop an image to a square already with my Extension (UIImage.imgToSquare(image: UIImage))....
But what I want, is the image SELECTION to always be square.
I'll attach a screenshot.
What I have:
What I want:
(the second one - what I want - would still be scrollable horizontally)
I think this will helpful for you..
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
var myimage:UIImage!
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView.contentMode = .ScaleAspectFit
myImageView.image = pickedImage
myimage = pickedImage
}
// So the user can pick only Square Image not others
if myimage.size.height == myimage.size.width
{
dismissViewControllerAnimated(true, completion: nil)
}
}
So I've got an imagePickerController didFinishPickingMediaWithInfo implementation where I'm taking an image from the Camera and storing it as a local file:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print(info)
if let image = info["UIImagePickerControllerOriginalImage"] {
if let data = UIImagePNGRepresentation(image as! UIImage) {
let filename = getDocumentsDirectory().stringByAppendingPathComponent("CurrentProfilePicture.png")
data.writeToFile(filename, atomically: true)
}
}
self.imageController.dismissViewControllerAnimated(true, completion: nil)
}
I'm then displaying the image to the user with the following code in ViewDidAppear:
override func viewDidAppear(animated: Bool) {
let filename = getDocumentsDirectory().stringByAppendingPathComponent("CurrentProfilePicture.png")
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filename) {
let Profileimage1 = UIImage(contentsOfFile: filename)
let ciImage = CIImage(image: Profileimage1!)
let orientation1: UIImageOrientation = (Profileimage1?.imageOrientation)!
profileImage.image = UIImage(CIImage: ciImage!, scale: 1.0, orientation:orientation1)
}
}
If I just load the UIImage straight into the UIImageView I get the incorrect orientation. I tried the above and I still get the incorrect orientation. The Photo was taken with the camera in portrait with the phone the correct way up and it comes out rotated 90 degrees counter clockwise. If I check the data returned from the info dictionary in the didFinishPickingMediaWithInfo method it states orientation 3 although I'm not sure what this relates to. When the image is loaded in ViewDidAppear the orientation that is returned is "Up" but this is obviously incorrect. If I turn the phone landscape the orientation is again wrong.
What am I doing wrong? Thanks!
I have a UIImagePickerController with the option to select from library.
When the accessLibrary button is called, it prompts another UIImagePickerController that has source type .PhotoLibrary.
func accessLibrary(sender: UIButton!) {
NSLog("access library")
choseFromLibrary = true
self.libraryPicker = UIImagePickerController()
self.libraryPicker!.allowsEditing = false
self.libraryPicker!.sourceType = .PhotoLibrary
self.presentViewController(self.libraryPicker!, animated: true, completion: nil)
}
This all works well, but when the user does select a photo, I want to pass that image to a view and then show that view on the screen.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary
var image = info[UIImagePickerControllerOriginalImage] as? UIImage
if (image != nil) {
if (choseFromLibrary) {
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
else {
if (frontFacing) {
image! = UIImage(CGImage: image!.CGImage!, scale: 1.0, orientation: .LeftMirrored)
}
let cnfrmImgView = ConfirmImageView.init(image: image!)
self.view.addSubview(cnfrmImgView)
}
}
}
The above function works if the image is taken using the camera, but not using the library.
When a user picks a photo from the library, the library picker controller just dismisses itself and the view is not display.
I ran an NSLog("here") to check if the didFinishPickingImageWithInfo function is called when choosing from the library and in fact it is not.
didFinishPickingImageWithInfo is a delegate message (UIImagePickerControllerDelegate). The problem, however, is that you have not given your image picker any delegate! Thus, it has no one to send delegate messages to.
if you want to implement a Instagram like image picker than try this
Image Picker like Instagram
The delegate function is updated in new swift versions from 4 to upwards.
Use this function instead
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { }
Besides browsing the Photolibrary I've enabled the Camera to add a photo to my
#IBAction func startCameraButtonTapped(sender: AnyObject) {
let startCameraController = UIImagePickerController()
startCameraController.delegate = self
startCameraController.sourceType = UIImagePickerControllerSourceType.Camera
startCameraController.allowsEditing = true
self.presentViewController(startCameraController, animated: true, completion: nil)
//invoiceImageHolder.image!.scaleAndRotateImage2(280)
//self.invoiceImageHolder.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
}
Every user input can be deleted, but now I'd like to know where the corresponding photo is. When I browse the photo library I can't see the photos taken with my app? Where are those photos stored?
[edit]
This is my saveImage process
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
saveImage(selectedImage, path: fileInDocumentsDirectory("\(uniqueImageName)"))
// set the unique image reference in textfield
// Set photoImageView to display the selected image
self.invoiceImageHolder.image = selectedImage
// Dismiss the picker
dismissViewControllerAnimated(true, completion: nil)
}
func saveImage(image: UIImage, path: String) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
// let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
let result = pngImageData?.writeToFile(path, atomically: true)
//print("Save result \(result)")
return result!
}
EDIT hmm I think I see where the photos are stored, but now how do I browse to them for deletions?
I think it's here:
/var/mobile/Containers/Data/Application/CBFE9E9D-A657-4011-8B9F-EF0C9BB2C603/Documents/
BUT everytime i restart the app the part between Application and /Documents is different??
Still not a working solution, but for those newbies as me this is a valuable readup: http://www.techotopia.com/index.php/Working_with_Directories_in_Swift_on_iOS_8
[EDIT 2]
with this code i am able to see all whiles stored with my app. I was a bit surprise to see all the files
// Files stored in my app filemanager
let filemgr = NSFileManager.defaultManager()
do {
print("Start filemanager")
let filelist = try filemgr.contentsOfDirectoryAtPath(documentsDirectory())
for filename in filelist {
print(filename)
}
} catch {
print("No directory path \(error)")
}
// end filemanager
UIImagePickerController() doesn't automatically save your photos into the library. See its UIImagePickerControllerDelegate protocol and you'll see that it has a method returning your UIImage after making a photo. Then you can save it:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
}
you save them - not in the photo library - but as 'simply' files inside your app's documents folder. There is no standard UI for deletion then.
You need to write your own ViewController to show & handle deletions then.