UIImageView缩放图像问题

时间:2016-09-04 23:00:18

标签: ios swift uiimageview scaling contentmode

我有一个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。

我希望它看起来像这样

screenshot1

但它看起来像这样

screenshot2

1 个答案:

答案 0 :(得分:0)

我无法理解你的问题。你能展示更多代码和故事板吗?

无论如何,这里是研究这个问题的例子:

  

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="stackoverflow_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" width="600" height="600"/>
                    <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" width="600" height="536"/>
                        </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" width="102" height="30"/>
                            <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" width="100" height="30"/>
                            <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" width="600" height="600"/>
                    <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" width="600" height="44"/>
                                    <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" width="600" height="43"/>
                                        <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" width="92" height="21"/>
                                                <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" width="320" height="44"/>
                    <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" width="5000" height="1246"/>
</resources>
</document>

<强>结果

enter image description here enter image description here

要导入的图像

enter image description here enter image description here

相关问题