简体   繁体   English

UIImageView中的UIImage位置移动

[英]UIImage position shifting in UIImageView

I have a UIImageView in a storyboard, into which I store UIImages from one of two sources - I either load the image from the net or from the local asset catalog. 我在情节提要中有一个UIImageView,我从其中两个来源之一存储UIImages-我从网络或从本地资产目录加载图像。 In all cases, by examination, the images are 48x48; 在所有情况下,通过检查,图像均为48x48; the image view happens to be 50x50. 图像视图恰好是50x50。 With the exception of the code that fetches the image (either via Alamofire+AlamofireImage or UIImage:named), the rest of the code is identical. 除了获取图像的代码(通过Alamofire + AlamofireImage或UIImage:named)外,其余代码都是相同的。 The images when loaded are, by examination in lldb, 48x48 for both sources. 通过在lldb中检查,两个源的图像在加载时均为48x48。

The behavior I see is that the images loaded from the net are displayed properly centered in the image view. 我看到的行为是从网络加载的图像在图像视图中正确显示在中心。 Those loaded from the asset catalog, however, are shifted over to the right, and are so shifted regardless of the content mode I set on the image view (I tried them all). 但是,从资产目录中加载的内容将移到右侧,并且无论我在图像视图上设置的内容模式如何(我都尝试了所有内容),它们都如此移动。 The entire local image is shifted over such that the left edge is not aligned to the left edge of the image view - it's roughly at the center. 整个本地图像会移动,以使左边缘不与图像视图的左边缘对齐-大致在中心。 As an experiment, I captured one of the net images and stored it in the catalog; 作为实验,我捕获了一张网络图像并将其存储在目录中。 it too was shifted when displayed, leading me to conclude the problem isn't the image itself. 它在显示时也发生了变化,使我得出结论,问题不在于图像本身。

My question is: what could be different in the two cases - between 48x48 images I load from the net versus identical images loaded from the asset catalog - that causes the locally loaded image to be shifted to the right? 我的问题是: 在这两种情况下-从网络加载的48x48图像与从资产目录加载的相同图像之间有什么不同-导致本地加载的图像向右移动?

This is the code to load from the asset catalog: 这是要从资产目录加载的代码:

image = UIImage(named: iconString)

I also tried this: 我也试过这个:

image = UIImage(named: iconString)?.imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, 0, 9))

but it made no difference. 但这没什么区别。

The Alamofire/AlamofireImage code is equally simple: Alamofire / AlamofireImage代码同样简单:

let request = Alamofire.request(.GET, url)
    .validate()
    .responseImage { response in completion?(result: response.result.value) }

and, finally, the code to put the image into the UI, common to both paths, is: 最后,用于将图像放入UI的代码(两条路径均相同)是:

cell.imageView?.image = fcsts.first!.icon

The experiment I did capturing the file and trying it from the asset catalog convinces me the file contents aren't the problem (although I'd be willing to be shown wrong!) - something is causing the locally-loaded image to be offset in the view. 我确实捕获了文件并在资产目录中尝试过的实验使我确信文件内容不是问题(尽管我很乐意显示为错误!)-某些原因导致本地加载的图像被偏移风景。

The application is for iPhone. 该应用程序适用于iPhone。 I'm testing on the latest Xcode/iOS; 我正在最新的Xcode / iOS上进行测试; the app is in Swift. 该应用程序在Swift中。

The problem turns out to be a function of automatic layout being done by the table view cell, as discussed here . 该问题被证明是由表视图细胞正在做自动布局的一个功能,如所讨论这里 I have explicitly set the icon's view size in my storyboard to be 50x50, so this override in the table cell class fixed the problem for me: 我已将故事板中图标的视图大小显式设置为50x50,因此表单元格类中的此覆盖为我解决了此问题:

override func layoutSubviews() {
    super.layoutSubviews()
    if let rect = self.imageView?.frame {
        var newRect = rect
        newRect.origin = CGPointMake((50-rect.size.width)/2, (50-rect.size.height)/2)
        self.imageView!.frame = newRect
    }
}

I have no explanation for why copying the image off the network to a local file causes it to be subject to the repositioning, nor for why the errant positioning is only in the horizontal direction even though all the sizing involved is symmetric. 对于为何将图像从网络上复制到本地文件,我将无法进行重新定位,也无法解释为什么即使所涉及的所有尺寸都是对称的,错误的定位也只能在水平方向上进行解释。 (Yes, it does work if I only adjust the X position; also adjusting Y is merely paranoia.) (是的,如果我仅调整X位置,它确实起作用;调整Y只是偏执狂。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM