I'm developing an ios app using swift4. When clicking profile image view, to set profile image on imageview, I have tap gesture call function.
The below is my code. I implemented picker delegate using extension following swift guide.
Seeing several documents about the same problem, I think I did what I have to do. But I can't figure out error, and set image on imageview. Please check my code.
import UIKit
import Kingfisher
import Alamofire
import Photos
class MyProfileVC: BaseNavigationVC {
@IBOutlet weak var stateMessageLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var nicknameLabel: UILabel!
@IBOutlet weak var sexImageView: UIImageView!
@IBOutlet weak var hash1Button: UIButton!
@IBOutlet weak var hash2Button: UIButton!
@IBOutlet weak var hash3Button: UIButton!
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tappedProfileImageView))
profileImageView.addGestureRecognizer(tap)
profileImageView.isUserInteractionEnabled = true
}
@objc func tappedProfileImageView(){
openGallery()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(false)
self.checkPermission()
//I updated this part. this code works correctly
if let imageUrl = UserInfo.shared().photoUri{
let url = URL(string: imageUrl)
profileImageView.layer.masksToBounds = false
profileImageView.clipsToBounds = true
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
profileImageView.contentMode = .scaleAspectFill
profileImageView.kf.setImage(with: url)
}
}
func openGallery(){
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized: print("Access is granted by user")
case .notDetermined: PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
print("success") }
})
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
}
extension MyProfileVC : UIImagePickerControllerDelegate,
UINavigationControllerDelegate{
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
self.profileImageView.image = image
print(info)
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
What would be cause of problem.
When I print info, debug area shows like this.
[__C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerReferenceURL): assets-library://asset/asset.JPG?id=54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B&ext=JPG, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerCropRect): NSRect: {{0, 0}, {2732.0000581881218, 2047.1787102689659}}, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerEditedImage): size {748, 560} orientation 0 scale 1.000000, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerMediaType): public.image, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerOriginalImage): size {2732, 2048} orientation 0 scale 1.000000, __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerPHAsset): 54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B/L0/001 mediaType=1/4, sourceType=1, (2732x2048), creationDate=2018-09-17 02:01:23 +0000, location=0, hidden=0, favorite=0 , __C.UIImagePickerControllerInfoKey(_rawValue: UIImagePickerControllerImageURL): file:///private/var/mobile/Containers/Data/Application/CD64C336-B5F1-48F6-9D54-504006ADD6E2/tmp/DD94F454-F3BE-4EF9-9F6F-0C90F61BEE19.jpeg]
It was my lack of programming thinking. Though I selected a photo from photo library, after the Activity choosing photo closed, viewWillAppear set the previous image on ImageView.