菜鸟教程小白 发表于 2022-12-12 09:22:32

ios - 中心和纵横比适合图像


                                            <p><p>目前我似乎无法同时设置 2 种内容模式。但我希望图像既缩放又居中。我该如何做到这一点?</p>

<pre><code>@property (weak, nonatomic) IBOutlet UIView *windowOut;

self.windowOut.layer.borderColor = .CGColor;
self.windowOut.layer.borderWidth = 3.0f;

UIImage *face = ;

UIImageView* imageView = [ initWithFrame:self.windowOut.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.contentMode = UIViewContentModeCenter;

;
;
</code></pre>

<p>这张图片是上面代码的结果:
<img src="/image/qKTBg.png" alt="enter image description here"/> </p>

<p>这个图片是"imageView.contentMode = UIViewContentModeCenter;
"被注释掉:
<img src="/image/zQenl.png" alt="enter image description here"/> </p>

<p>编辑:
此图像是我注释掉两个 contentModes 时的结果:
<img src="/image/8yWzz.png" alt="enter image description here"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>不居中的原因在这里:</p>

<pre><code>UIImageView* imageView = [ initWithFrame:self.windowOut.frame];

;
</code></pre>

<p>仅当 <code>windowOut</code> 恰好位于 (0/0) 时才有效。
<code>.frame</code> 在其父 View 的坐标系中给出框架。如果 <code>windowOut</code> 位于,假设 (20/20) 在其父 View(self.view?) 中,那么 <code>imageView</code> 将位于 <strong>(40,40) </strong>。 </p>

<p>使用 <code>bounds</code> 代替。这是相同的,但在它自己的坐标系内。 </p>

<pre><code>UIImageView* imageView = [ initWithFrame:self.windowOut.bounds];
;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 中心和纵横比适合图像,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23066092/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23066092/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 中心和纵横比适合图像