【发布时间】:2016-01-10 12:43:28
【问题描述】:
我正在尝试从 URL 中替换图像(如果存在)。 问题似乎是我无法设置 UIImageView 的大小。
我的代码如下所示:
UIImageView *partnerIcon = (UIImageView*)[cell viewWithTag:0];
NSURL *imageUrl = [NSURL URLWithString:[[@"http://www.fitpas.ch/coreapp/resources/images/center/" stringByAppendingString:[[result objectAtIndex:indexPath.row] objectForKey:@"cid"]] stringByAppendingString:@".jpg"]];
UIImage* partnerImage = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]];
if (partnerImage != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
//change image
partnerIcon.image = partnerImage;
partnerIcon.contentMode = UIViewContentModeScaleToFill;
});
}
这会导致:
在最后一行的图片中,partnerImage 为 nil,这表明它应该是这样的。
我尝试使用缩放 UIImage
partnerIcon.image = [UIImage imageWithCGImage:partnerImage.CGImage
scale:1/100 orientation:partnerImage.imageOrientation];
但这不会改变任何事情。
我还尝试设置以更改 UIImageView 的尺寸:
partnerIcon.bounds = CGRectMake(0, 0, 50, 50);
还有
partnerIcon.frame = CGRectMake(0, 0, 50, 50);
但这也不起作用。
【问题讨论】:
-
你在使用自动布局吗?
-
是的,但我认为我需要这样做,因为单元格中有更多视图
-
是的,使用自动布局是绝对正确的方法。但是
frame使用是不能改的,需要把布局约束连接成outlets,改改。 -
我不明白为什么框架会自动改变,基本上我不想改变它,如果它可以工作。我只想替换 imageview 中的图像,这似乎不起作用。
-
[cell viewWithTag:0]标记零由系统保留。你不应该使用它。这只是一个建议,而不是您问题的解决方案。
标签: ios objective-c uiimageview cgrectmake contentmode