我有UIBarButton,用户可以更改图像,只需使用图像选择器选择照片或图库图像即可。我的问题是比例尺图像不好。
如果我使用AspectFit,则UIBarButton如下所示:
如果我使用AspectFill,则我的UIBarButton如下所示:
如果我先尝试更改尺寸图像,然后再设置图像,那么图像就会一直刮擦:
这是我的代码:
{
"name": "angular",
"version": "1.0.0",
"description": "",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@agm/core": "^1.0.0-beta.5",
"@angular/animations": "^7.1.2",
"@angular/common": "^7.1.2",
"@angular/compiler": "^7.1.2",
"@angular/core": "^7.1.2",
"@angular/forms": "^7.1.2",
"@angular/http": "^7.1.2",
"@angular/platform-browser": "^7.1.2",
"@angular/platform-browser-dynamic": "^7.1.2",
"@angular/router": "^7.1.2",
"core-js": "^2.5.4",
"moment": "^2.22.2",
"ngx-bootstrap": "^3.0.0",
"ngx-moment": "^3.1.0",
"rxjs": "^6.3.3",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.6.3",
"@angular/cli": "^7.1.2",
"@angular/compiler-cli": "^7.1.2",
"@angular/language-service": "^7.1.2",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"fs-extra": "^7.0.0",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-clean-css": "3.4.0",
"gulp-concat": "2.6.1",
"gulp-less": "^3.5.0",
"gulp-minify": "2.1.0",
"gulp-rev": "^8.1.1",
"gulp-sass": "^3.1.0",
"gulp-typescript": "^3.2.3",
"gulp-uglify": "3.0.0",
"jasmine": "^3.1.0",
"jasmine-core": "~2.99.1",
"jasmine-reporters": "^2.3.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"less": "2.7.3",
"node-sass": "^4.11.0",
"protractor": "~5.3.0",
"protractor-html-reporter": "^1.3.2",
"ts-node": "~5.0.1",
"tslint": "^5.11.0",
"typescript": "~3.1.6"
}
}
这里是func来调整图像大小:
func createPhotoBarButton(image: Data) {
let barbtn = UIBarButtonItem()
var image = UIImage(data:image)
if image == nil {
image = UIImage(named: "photoIcon")
}
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))
imageView.image = image?.resizedImage(newSize: CGSize(width: 35, height: 35))?.withRenderingMode(.alwaysOriginal)
// imageView.image = cropToBounds(image: image!, width: 35, height: 35)
// imageView.image = image
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = imageView.frame.size.height / 2
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
self.navigationItem.rightBarButtonItem = barbtn
barbtn.customView = imageView
barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
}
请帮助我找到解决问题的正确方法。
答案 0 :(得分:1)
据我了解您的问题。我找到了解决方案。
尝试
func createPhotoBarButton(image: Data) {
let barbtn = UIBarButtonItem()
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0))
var image = UIImage(data:image)
if image == nil {
image = UIImage(named: "photoIcon")?.resize(maxWidthHeight: Double(imageView.frame.size.width))
}
imageView.image = image
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = imageView.frame.size.height / 2
imageView.layer.masksToBounds = true
imageView.clipsToBounds = true
self.navigationItem.rightBarButtonItem = barbtn
barbtn.customView = imageView
barbtn.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(photoTapped(_:))))
}
调整大小方法
extension UIImage {
func resize(maxWidthHeight : Double)-> UIImage? {
let actualHeight = Double(size.height)
let actualWidth = Double(size.width)
var maxWidth = 0.0
var maxHeight = 0.0
if actualWidth > actualHeight {
maxWidth = maxWidthHeight
let per = (100.0 * maxWidthHeight / actualWidth)
maxHeight = (actualHeight * per) / 100.0
}else{
maxHeight = maxWidthHeight
let per = (100.0 * maxWidthHeight / actualHeight)
maxWidth = (actualWidth * per) / 100.0
}
let hasAlpha = true
let scale: CGFloat = 0.0
UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: maxHeight), !hasAlpha, scale)
self.draw(in: CGRect(origin: .zero, size: CGSize(width: maxWidth, height: maxHeight)))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
return scaledImage
}
}
输出