【问题标题】:Adjusting UIImageView size and location based on the size of image fetched from service根据从服务中获取的图像大小调整 UIImageView 大小和位置
【发布时间】:2014-03-04 10:29:03
【问题描述】:

在我的 iOS7 应用程序中,我可以查看任何社交网站中的个人资料页面。我在左上角有一张个人资料图片。我正在从服务 url 获取图像并将其加载到 UIImageview

我的问题是,当服务图像的大小很大时,我必须增加这个图像视图的大小。而且我还必须在这个图像视图旁边调整其他UILabels。我正在使用此代码从 url 获取图像的大小:

NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
CGSize size = img.size;

【问题讨论】:

  • 好的,那么您是如何尝试更改大小和位置的呢?你在使用自动布局吗?出了什么问题?
  • 你可以设置imageView的contentMode。 [self.imageView setContentMode:UIViewContentModeScaleAspectFill]; 这将使图像适合您拥有的图像视图。
  • @Wain,根据我的 UIImageview 大小拉伸了图像。但是我从服务中获得了不同尺寸的图像。有时服务返回具有方形尺寸的图像,有时返回矩形尺寸的图像。如果是矩形,则图像被压缩。
  • 将你的imageView的尺寸设置为与从服务中获取的image的尺寸相同。
  • 好问题。我也面临同样的问题。等待好的答案

标签: objective-c ios7 uiimageview


【解决方案1】:

我不知道 UIImage 类上的initWithData:cache: 方法,是这个类的东西吗?

无论如何,您可以通过[imageView sizeToFit] 使 UIImageView 尺寸适合内容 UIImage。 如果要调整比例,请使用[UIImage imageWithData:data scale:scale]

NSString *path = @"http://www.example.com/example@x2.png"
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data scale:2.0];
_imageView.image = img;
[_imageView sizeToFit];

【讨论】:

    猜你喜欢
    • 2017-08-19
    • 2012-06-22
    • 2013-08-28
    • 2018-01-25
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多