iOS 11中的UITableViewCell转换中断

时间:2017-10-28 00:31:24

标签: ios objective-c uitableview cgaffinetransform

在我构建的应用程序中,我有一个表格视图,通过在cellForRowAtIndexPath中运行以下代码来显示每个单元格左侧的图像:

cell.imageView.image = [UIImage imageNamed:myImage];

但是图像太大了,所以我缩小了它:

cell.imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);

这在iOS 10中运行得很好。但是一旦我使用iOS 11 SDK升级到最新的Xcode,图像变得非常庞大。事实证明,转换图像视图的第二行代码现在什么都不做:我可以将它注释掉,将0.3改为其他等等,它没有任何区别。 CGAffineTransformMakeScale在新的Xcode中仍然有文档,所以我假设它没有被弃用,但是为什么这会打破iOS 11,我该如何修复呢?有任何想法吗?提前致谢!请注意,我正在使用Objective-C。

修改

刚刚对代码进行了3次更改:

  1. 将第二行更改为cell.imageView.transform = CGAffineTransformMakeScale(0.0000001, 0.0000001);。没有任何事情发生(即图像视图和它们内部的图像仍然一样巨大)。

  2. 将第二行更改为cell.imageView.transform = CGAffineTransformMakeScale(0, 0);。图像从图像视图中消失,但图像视图的大小仍然相同,您可以告诉它,因为它仍会替换单元格中的文本并将其推向右侧。

  3. 删除第一行代码(不再将图像分配给imageview)。 imageview消失,文本一直移动到单元格的左侧。

  4. 也许这有助于揭示正在发生的事情?

2 个答案:

答案 0 :(得分:1)

因此,如果您尝试调整图片大小以适合if len(sys.argv) == 1: #do something elif len(sys.argv) == 2: #do something else elif len(sys.argv) == 3: #do something different else: #do the last possibility ,您应该实际使用imageView' imageView属性,如下所示:

contentMode

或在Swift中为其他人

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

这可以保留图像的尺寸,并使最大尺寸的图像适合cell.imageView.contentMode = .scaleAspectFit 而不改变尺寸

您还可以尝试imageView(或Swift中的UIViewContentModeScaleAspectFit),它基本上完全填充.scaleAspectFill的尺寸,但如果图片比图片视图宽或高什么是不合适的。

以下是来自Obj-C和Swift源文件的所有contentModes:

imageView

编辑:

因为我看到你也对改变typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped. UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay) UIViewContentModeCenter, // contents remain same size. positioned adjusted. UIViewContentModeTop, UIViewContentModeBottom, UIViewContentModeLeft, UIViewContentModeRight, UIViewContentModeTopLeft, UIViewContentModeTopRight, UIViewContentModeBottomLeft, UIViewContentModeBottomRight, }; public enum UIViewContentMode : Int { case scaleToFill case scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent case scaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped. case redraw // redraw on bounds change (calls -setNeedsDisplay) case center // contents remain same size. positioned adjusted. case top case bottom case left case right case topLeft case topRight case bottomLeft case bottomRight } 本身的尺寸感兴趣("为文本留下更多空间")我建议实际上是,故事板或以编程方式使用AutoLayout添加您自己的imageView并使其大小和放置您想要的大小,而不是在单元格上使用默认的imageView,这是一个方便/标准化的工具。

如果您不熟悉,我会谷歌搜索AutoLayout教程。或者也许"使用AutoLayout创建自定义UITableViewCell"

如果你真的不想创建自己的子类,你可以尝试设置`cell.imageView.frame = ..."在某处手动将其调整为您想要的大小,然后设置其内容模式以确保图像仍能很好地适应。

请参阅此问题:How do I make UITableViewCell's ImageView a fixed size even when the image is smaller

答案 1 :(得分:1)

找到了我自己的问题的答案,由于保罗​​在这个问题上给出了回答,我得到了答案:How to resize a cell.imageView in a TableView and apply tintColor in Swift

let parameters = ["category":"15" as AnyObject]

let headers: HTTPHeaders = [
    "Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl",
    "Accept": "application/json"
]

Alamofire.upload(multipartFormData: { multipartFormData in

    multipartFormData.append(fileUrl , withName: "image" , fileName: yourfilename + ".yourfiletype", mimeType: "yourfiletype")

    for (key, value) in parameters {
        multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
    }

},to: "https://woo.demoapp.xyz/wp-json/wc/v2/products", method: .post, headers: headers ,
  encodingCompletion: { encodingResult in
    switch encodingResult {
    case .success(let upload, _, _):
        upload.response { [weak self] response in
            guard self != nil else {
                return
            }

        }

        upload.responseJSON {  response in

            let responseJSON = response.result.value as! NSDictionary


        }

    case .failure(let encodingError):
        print("error:\(encodingError)")

    }


})

我仍然不知道为什么旧的CGAffineTransformMakeScale不再起作用,但这可以完成工作。