OGeek|极客世界-中国程序员成长平台

标题: ios - 图像适合 UICollectionView 单元格 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:56
标题: ios - 图像适合 UICollectionView 单元格

我正在使用 collectionview 来显示我从服务器获取的产品。但是图像不适合 collectionview 单元格。我想知道有没有办法解决它?

NSURL *url = [NSURL URLWithString"http://www.myURL/productAll.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSError *myError = nil;
    id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];        
    items = [[NSMutableArray alloc] init];
    for (int i=0; i<[res count]; i++) {
        NSString *arrayResult = [[res objectAtIndex:i]objectForKey"image"];            
        NSData *data = [[NSData alloc] initWithBase64EncodedString:arrayResult options:NSDataBase64DecodingIgnoreUnknownCharacters];
        UIImage *captcha_image = [[UIImage alloc] initWithData:data];
        [items addObject:captcha_image];
    }
    [collectionView reloadData];
}];

这里是 collectionView 单元格设计,imageView 尺寸与 collectionView 单元格匹配。 enter image description here

这是用户看到的:

enter image description here

这是来自 ImageView 的额外信息

enter image description here

enter image description here

cellCollectionView

enter image description here



Best Answer-推荐答案


首先,现代 Apple 设备有各种尺寸。当你只为一个屏幕设计时,已经不是 2010 年了。移至自动布局。
其次,您可以在下面找到有关如何进行所有正确设置的非常详细的说明。

  1. 点击您的图片 View
  2. 点击引脚约束按钮
  3. 放下常量(与边距相同)并选择/突出显示红色连接器框到数字
  4. 取消选中“限制边距”复选框
  5. 点击添加 4 个约束按钮
  6. 确保您的 ImageView 模式是 Aspect Fill
  7. 不要忘记选中重要的“剪辑 subview ”复选框,除非您会看到相互放大的图像

Detailed explanation for adding constants

Detailed explanation for Image View setup

关于ios - 图像适合 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266002/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4