我正在使用swift4开发ios应用程序。 单击个人资料图像视图时,要在imageview上设置个人资料图像,我具有点击手势调用功能。
以下是我的代码。 我按照快速指南使用扩展程序实现了选择器委托。
看到有关同一问题的多个文档,我想我做了我必须做的。但是我无法弄清楚错误,并在imageview上设置图像。 请检查我的代码。
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)
}
}
什么会引起问题。
当我打印信息时,调试区域将显示如下。
[__ C.UIImagePickerControllerInfoKey(_rawValue:UIImagePickerControllerReferenceURL):资产图书馆://asset/asset.JPG?id = 54FE2CEF-59E7-4BCC-AF84-D64C1F32E58B&ext = JPG,__C.UIImagePickerControllerInfoKey(_rawValue:UIImagePickerController {{0,0},{2732.0000581881218,2047.1787102689659}},__C.UIImagePickerControllerInfoKey(_rawValue:UIImagePickerControllerEditedImage):大小{748,560}方向0缩放1.000000,__C.UIImagePickerControllerInfoInfoKey(_rawValue:UIImagePickerControllerMediaType):public.Image,__C。 (_rawValue:UIImagePickerControllerOriginalImage):大小{2732,2048}方向0比例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:/// pr ivate / var / mobile /容器/数据/应用程序/CD64C336-B5F1-48F6-9D54-504006ADD6E2/tmp/DD94F454-F3BE-4EF9-9F6F-0C90F61BEE19.jpeg]
答案 0 :(得分:0)
这是我缺乏编程思想的原因。 尽管我从照片库中选择了一张照片,但是在“选择照片的活动”关闭后,viewWillAppear会在ImageView上设置上一张图像。