UIImageView 缩放图像问题
Posted
技术标签:
【中文标题】UIImageView 缩放图像问题【英文标题】:UIImageView scaling image issue 【发布时间】:2016-09-04 23:00:17 【问题描述】:我有一个宽度远大于高度的 UIImage。我试图在保持纵横比的同时在 UIImageView 中显示它,但 imageview 只显示图像的一小部分。我做错了吗?
func scale(image:UIImage)
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.image = image
print(imageView.frame.size.width)
print(image.size.width)
在控制台打印出来:
250.0
3024.0
image.size.height 的值为 150。
我希望它看起来像这样
但它看起来像这样
【问题讨论】:
您希望它看起来如何?可以分享一些截图吗? 为截图编辑了我的帖子 可能需要查看整个 View Controller 类。 您是从viewDidLoad
调用scale
,如果是,请尝试从viewDidLayoutSubviews
调用它,而不是使用自动布局。
我没有使用自动布局。我的 UIImageView 仅居中对齐并设置为 600 宽度和 250 高度。
【参考方案1】:
我无法理解您的问题。你能展示更多的代码和故事板吗?
不管怎样,这里是研究问题的例子:
ViewController.swift
import UIKit
class ViewController: UIViewController
@IBOutlet var imageView: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func SelectImage(sender: UIButton)
switch sender.tag
case 0:
imageView.image = UIImage(named: "image1")
case 1:
imageView.image = UIImage(named: "image2")
default:
break
Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="ios.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="mG9-Ie-ulu">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="phU-zf-Z59">
<objects>
<viewController id="e22-se-1BX" customClass="ViewController" customModule="***_39322193" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="ieU-T4-sfY"/>
<viewControllerLayoutGuide type="bottom" id="pBS-7o-IXo"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="GUS-lS-q7w">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="image1" translatesAutoresizingMaskIntoConstraints="NO" id="upT-By-1gT">
<rect key="frame" x="0.0" y="20" />
</imageView>
<button opaque="NO" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="j8O-MB-VWm">
<rect key="frame" x="479" y="562" />
<state key="normal" title="Select Image 2"/>
<connections>
<action selector="SelectImage:" destination="e22-se-1BX" eventType="touchUpInside" id="uLd-Po-8ev"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="agx-9V-LpP">
<rect key="frame" x="20" y="562" />
<state key="normal" title="Select Image 1"/>
<connections>
<action selector="SelectImage:" destination="e22-se-1BX" eventType="touchUpInside" id="o9C-8u-JxX"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="agx-9V-LpP" firstAttribute="leading" secondItem="GUS-lS-q7w" secondAttribute="leadingMargin" id="Gx9-Lx-S7I"/>
<constraint firstAttribute="trailing" secondItem="upT-By-1gT" secondAttribute="trailing" id="Hji-6d-ORN"/>
<constraint firstAttribute="trailingMargin" secondItem="j8O-MB-VWm" secondAttribute="trailing" constant="-1" id="Jst-1T-PP4"/>
<constraint firstItem="agx-9V-LpP" firstAttribute="top" secondItem="upT-By-1gT" secondAttribute="bottom" constant="6" id="STt-vV-su8"/>
<constraint firstItem="agx-9V-LpP" firstAttribute="top" secondItem="j8O-MB-VWm" secondAttribute="top" id="Uhl-PA-XLU"/>
<constraint firstItem="upT-By-1gT" firstAttribute="leading" secondItem="GUS-lS-q7w" secondAttribute="leading" id="Xay-ar-ZB2"/>
<constraint firstItem="upT-By-1gT" firstAttribute="top" secondItem="GUS-lS-q7w" secondAttribute="top" constant="20" symbolic="YES" id="ZT9-p7-2vo"/>
<constraint firstItem="pBS-7o-IXo" firstAttribute="top" secondItem="agx-9V-LpP" secondAttribute="bottom" constant="8" symbolic="YES" id="cRT-De-cHc"/>
<constraint firstItem="agx-9V-LpP" firstAttribute="baseline" secondItem="j8O-MB-VWm" secondAttribute="baseline" id="zc4-Dn-9Iu"/>
</constraints>
</view>
<connections>
<outlet property="imageView" destination="upT-By-1gT" id="Z9C-40-UW3"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Bqi-Ne-i08" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1116" y="517"/>
</scene>
<!--Root View Controller-->
<scene sceneID="ywS-Tj-ArZ">
<objects>
<tableViewController id="Ixy-MR-Jzp" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="qeH-hZ-NWL">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<sections>
<tableViewSection id="z96-NS-xWx">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="z9h-YZ-NYn">
<rect key="frame" x="0.0" y="64" />
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="z9h-YZ-NYn" id="MBf-LU-nOF">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="show image" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="taK-E6-9Tb">
<rect key="frame" x="254" y="11" />
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="taK-E6-9Tb" firstAttribute="centerY" secondItem="MBf-LU-nOF" secondAttribute="centerY" id="Uxk-t1-uBI"/>
<constraint firstItem="taK-E6-9Tb" firstAttribute="centerX" secondItem="MBf-LU-nOF" secondAttribute="centerX" id="fqV-Xt-MGV"/>
</constraints>
</tableViewCellContentView>
<connections>
<segue destination="e22-se-1BX" kind="show" identifier="showImageView" id="fpv-Ij-HJm">
<nil key="action"/>
</segue>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
<connections>
<outlet property="dataSource" destination="Ixy-MR-Jzp" id="erF-5c-clc"/>
<outlet property="delegate" destination="Ixy-MR-Jzp" id="2Hw-QW-7TB"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Root View Controller" id="Lrj-E3-lJl"/>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="T9r-3a-rLA" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="328" y="517"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="hl3-CB-blQ">
<objects>
<navigationController id="mG9-Ie-ulu" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="ZhA-OA-J4s">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="Ixy-MR-Jzp" kind="relationship" relationship="rootViewController" id="h4s-cf-6cE"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="it8-XA-fgK" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-492" y="517"/>
</scene>
</scenes>
<resources>
<image name="image1" />
</resources>
</document>
结果
要导入的图片
【讨论】:
您找到问题的解决方案了吗?以上是关于UIImageView 缩放图像问题的主要内容,如果未能解决你的问题,请参考以下文章
在 UIImageView 内的 UIImage 上缩放和居中