Home>
I want to solve the problem that when I select an image in the library in Swift, the image is duplicated
How can I prevent it from being duplicated?
import UIKit
// Select an image
extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// Process when image is selected
func imagePickerController (_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
let selectedImage = info [UIImagePickerController.InfoKey.originalImage] as! UIImage
self.imageView.image = image
UIImageWriteToSavedPhotosAlbum (image, nil, nil, nil)
// compress the size
// let resizedImage = selectedImage.scale (byFactor: 0.4)
image = selectedImage
var imageHeight = image.size.height
var imageWidth = image.size.width
let navigationBarHeight = navigationController? .navigationBar.frame.height
let width = self.view.frame.width
let height = self.view.frame.height
let centerX = self.view.center.x
let centerY = self.view.center.y
let widthRatio = imageWidth
let heightRatio = imageHeight
// Change the size of iamgeview according to the size of the image
if imageHeight>self.view.frame.height || imageWidth>self.view.frame.width {
print ("1")
imageWidth = width
imageHeight = width * heightRatio/widthRatio
} else if imageHeight>self.view.frame.height {
print ("2")
imageHeight = height
imageWidth = height * widthRatio/heightRatio
} else if imageWidth>self.view.frame.width {
print ("3")
imageWidth = width
imageHeight = width * heightRatio/widthRatio} else {
}
imageView.contentMode = UIView.ContentMode.scaleToFill
imageView.frame.size = CGSize (width: imageWidth, height: imageHeight)
// Do not put the image on the navigationbar
if imageHeight/2>(height/2-navigationBarHeight!) {
print ("4")
imageView.center = CGPoint (x: centerX, y: centerY + navigationBarHeight!)
} else {
print ("5")
imageView.center = CGPoint (x: centerX, y: centerY)
}
imageView.image = image
picker.dismiss (animated: true, completion: nil)
}
// Called when shooting is canceled
func imagePickerControllerDidCancel (_ picker: UIImagePickerController) {
picker.dismiss (animated: true, completion: nil)
}
func tappedlibrary () {
let sourceType: UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary
if UIImagePickerController.isSourceTypeAvailable (UIImagePickerController.SourceType.photoLibrary) {
// create an instance
let cameraPicker = UIImagePickerController ()
cameraPicker.sourceType = sourceType
cameraPicker.delegate = self
self.present (cameraPicker, animated: true, completion: nil)
}
else {
print ("error")
}
}
func tappedcamera () {
let sourceType: UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera// Check if the camera is available
if UIImagePickerController.isSourceTypeAvailable (UIImagePickerController.SourceType.camera) {
// create an instance
let cameraPicker = UIImagePickerController ()
cameraPicker.sourceType = sourceType
cameraPicker.delegate = self
self.present (cameraPicker, animated: true, completion: nil)
}
else {
print ("error")
}
}
@IBAction func selecteImageButton (_ sender: UITapGestureRecognizer) {
// For alert display
let actionSheet = UIAlertController (title: "", message: "Select photo", preferredStyle: UIAlertController.Style.actionSheet)
let tappedcamera = UIAlertAction (title: "Shoot with camera", style: UIAlertAction.Style.default, handler: {
(action: UIAlertAction!) in
self.tappedcamera ()
})
let tappedlibrary = UIAlertAction (title: "Select from library", style: UIAlertAction.Style.default, handler: {
(action: UIAlertAction!) in
self.tappedlibrary ()
})
let cancel = UIAlertAction (title: "Cancel", style: UIAlertAction.Style.cancel, handler: {
(action: UIAlertAction!) in
print ("Cancel")
})
actionSheet.addAction (tappedcamera)
actionSheet.addAction (tappedlibrary)
actionSheet.addAction (cancel)
present (actionSheet, animated: true, completion: nil)
}
}
-
Answer # 1
-
Answer # 2
UIImageWriteToSavedPhotosAlbum (image, nil, nil, nil)
Please comment this out.
This code saves a photo in an album.
Related articles
- swift - to screen transition by image data
- swift - reflection of cell height and image
- swift - i want to set an image using segmentedcontrol
- swift - i want to paste an image at the cursor position of textview
- swift - i want to paste two or more images from text library to textview
- swift - change color when selecting collectionview cell
- [swift] i want to display an image in the message of uialertcontroller
- [swift] i don't understand the meaning of? and: used for image reduction processing (scale)
- ios - [swift] blue check mark does not appear when selecting a cell in edit mode of uitableview
- swift - i want to solve the error when selecting the first one in the picker view
- react native - image enlargement library
- swift - when i try to display an image on actionsheet, only a blue square is displayed
- swift - i want to reflect on label after selecting a value with picker
- swift - i want to retrieve an image using dkimagepickercontroller
- ios - [swift] i want to save the image and video values in firebase storage and then save the url in cloud firestore
- how to crop an image with javascript without using a library or plugin
- swift - pass the screenshot image to the transition destination screen
- [swift ui] image image and text characters shift when screen transitions are repeated * there is an image *
- swift - about ar image
- xcode - i want to display text on top of an image in swift, but i get an exception
Trends
UIImageWriteToSavedPhotosAlbum
?What if I comment out this one line?