问题描述
我正在将imageView放置在视图中。
containerView约束
top = 40,leading = 0,trailing = 0,height = 180
带有containerView的imageView约束
top = 0,bottom = 0
我正在从图库代码中获取图像:我正在使用图像选择器获取图像
class FinalViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var picContainerView: UIView!
var picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden=true
}
@IBAction func profileButton(_ sender: Any) {
let actionSheetController: UIAlertController = UIAlertController(title: nil,message: nil,preferredStyle: .actionSheet)
actionSheetController.popoverPresentationController?.sourceView = self.picContainerView
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel",style: .cancel) { action -> Void in
}
actionSheetController.addAction(cancelAction)
let takePictureAction: UIAlertAction = UIAlertAction(title: "TakePhoto",style: .default) { action -> Void in
self.openCameraPicker()
}
actionSheetController.addAction(takePictureAction)
let choosePictureAction: UIAlertAction = UIAlertAction(title: "ChooseFromLibrary",style: .default) { action -> Void in
self.openPhotoGallery()
}
actionSheetController.addAction(choosePictureAction)
//Present the
self.present(actionSheetController,animated: true,completion: nil)
}
func openCameraPicker() {
picker.sourceType = UIImagePickerController.SourceType.camera
picker.cameraCaptureMode = .photo
picker.allowsEditing = true
picker.modalPresentationStyle = .fullScreen
present(picker,completion: nil)
}
func openPhotoGallery() {
picker.sourceType = .photoLibrary
picker.allowsEditing = true
picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker,completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let img = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
profileImg.image = img
}
else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.profileImg.image = image
}
dismiss(animated: true,completion: nil)
}
}
如果我给AspectFit
,则o / p ..图像以及空白
如果我给AspectFill
,则o / p ..图片会拉伸
如何根据图像调整imageview的大小。请帮助我提供代码
解决方法
首先尝试将ContainerView的背景设置为清晰而不是白色。 如果这不起作用,请尝试以下操作:
UIImage *img = [UIImage imageNamed:@"logo.png"];
CGFloat width = img.size.width;
CGFloat height = img.size.height;
然后将ImageView的高度和宽度设置为ContainerViews的宽度和高度。