我有一个UIImageView,目标是通过给它一个高度或宽度按比例缩小它.
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//Add image view
[self.view addSubview:imageView];
//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;
//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;
Run Code Online (Sandbox Code Playgroud)
图像确实调整了大小但位置不在左上角.缩放image/imageView的最佳方法是什么?如何更正位置?
我有一堆用于细胞图像视图的图像,它们都不超过50x50.例如40x50,50x32,20x37 .....
当我加载表格视图时,文本不会排列,因为图像的宽度会有所不同.此外,我希望小图像出现在中心而不是左侧.
这是我在'cellForRowAtIndexPath'方法中尝试的代码
cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
cell.imageView.autoresizesSubviews = NO;
cell.imageView.contentMode = UIViewContentModeCenter;
cell.imageView.bounds = CGRectMake(0, 0, 50, 50);
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.image = [UIImage imageWithData: imageData];
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我尝试了一些东西,但它们都没有用.