Reputation: 827
I am creating a simple camera app which saves a photo after the user has pressed the camera button into a variable called image and than into the photo library.
Next the app moves on to another view controller which shows the image in a square image view. This all works, but before the image is shown the user should be able to choose the exact position of the photo, which he would be able to do when the UIImagePicker is shown.
The image illustrates View 1 (the camera) and View 2 (the final view controller showing the image). After the user pressed the camera button and the image is saved to the library, I'd like to call the UIImagePicker and automatically show the last image, so that the user can move the position of the content he wants to show in the squared image view. Than the UIImagePicker is closed and the image is shown in View 2 in my squared image view.
Is that possible?
View 1:
Here is how I save the image so far and go to the next view:
@IBAction func captureImage(_ sender: UIButton) {
cameraController.captureImage {(image, error) in
guard let image = image else {
print(error ?? "Image capture error")
return
}
try? PHPhotoLibrary.shared().performChangesAndWait {
PHAssetChangeRequest.creationRequestForAsset(from: image)
print("photo saved")
}
// go to next view controller and transfer image to inputImage
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Filter") as! FilterViewController
vc.inputImage = image!
self.navigationController?.pushViewController(vc, animated: true)
}
Obviously I could just show the new image in the squared image view, but since the user takes a fullscreen photo, the content he wants to see might not be in the center, so choosing the exact position is mandatory and thats where I want to display the UIImagePicker with the last taken photo (the user should not be able to choose another image).
View 2:
So far I just load the UIImagePicker when view 2 is loaded in viewDidLoad(), but obviously it asks the user to choose an image:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {return}
imageView.image = image
inputImage = image
dismiss(animated: true) {
// do something here...
}
}
Upvotes: 0
Views: 470
Reputation: 535889
I would suggest that your attempt to use UIImagePickerController in the second slot in your view controller sequence diagram is totally misguided. You want to force a particular image on the user. The UIImagePickerController is not about that. It's about letting the user pick an image (hence the name, Image Picker). [Alternatively, it's about letting the user capture an image with the camera, but you're not using it for that.]
So I'd suggest using your own custom view controller for this second slot in the diagram. Designing a cropping interface (if that's your aim) is not difficult.
Upvotes: 3