在iOS中如何在单元格图像的中心添加图像

在iOS中如何在单元格图像的中心添加图像,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我想将图像添加到细胞图像的中心。我有细胞图像,因此如果细胞是视频,那么我想在细胞图像的中心添加播放图标。我尝试了以下代码,但无效。请告诉我如何解决此问题 if([user_post.post_image isEqualToString:@"none"]) { [cell.img_post_thumbnail setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingStr

我想将图像添加到细胞图像的中心。我有细胞图像,因此如果细胞是视频,那么我想在细胞图像的中心添加播放图标。我尝试了以下代码,但无效。请告诉我如何解决此问题

  if([user_post.post_image isEqualToString:@"none"])
        {
            [cell.img_post_thumbnail setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_post.post_video_thumbnail]] placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];
            CGPoint centerImageView = cell.img_post_thumbnail.center;
            centerImageView.x = self.view.center.x;
            centerImageView.y = self.view.center.y;

            UIImageView *playIcon=[[UIImageView alloc]initWithFrame:CGRectMake(centerImageView.x, centerImageView.y,20,20)];
            playIcon.image=[UIImage imageNamed:@"play_icon.png"];
            [cell.img_post_thumbnail addSubview:playIcon];




        }
编辑: 图像居中,但未按给定尺寸缩放

[cell.img_post_thumbnail setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_post.post_video_thumbnail]] placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];


float btn_width = cell.frame.size.width * (5/ 320.0);
float btn_height = cell.frame.size.height * (5 / 568.0);

UIImageView *playIcon=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10,btn_width,btn_height)];
playIcon.image=[UIImage imageNamed:@"play_icon.png"];
playIcon.translatesAutoresizingMaskIntoConstraints = NO;
[cell.img_post_thumbnail addSubview:playIcon];


float btn_selfRatio=(btn_width/btn_height);
float btn_superRatio=(btn_width/playIcon.frame.size.height);

[playIcon addConstraint:[NSLayoutConstraint constraintWithItem:playIcon attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:playIcon attribute:NSLayoutAttributeHeight multiplier:btn_selfRatio constant:0]];

[cell.img_post_thumbnail addConstraint:[NSLayoutConstraint constraintWithItem:playIcon attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:cell.img_post_thumbnail attribute:NSLayoutAttributeHeight multiplier:btn_superRatio constant:0]];


[[cell img_post_thumbnail] addConstraint:
 [NSLayoutConstraint constraintWithItem:playIcon
                              attribute:NSLayoutAttributeCenterX
                              relatedBy:NSLayoutRelationEqual
                                 toItem:[cell img_post_thumbnail]
                              attribute:NSLayoutAttributeCenterX
                             multiplier:1.0
                               constant:0.0]];

[[cell img_post_thumbnail] addConstraint:
 [NSLayoutConstraint constraintWithItem:playIcon
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:[cell img_post_thumbnail]
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1.0
                               constant:0.0]];
试试这个

playIcon.center = cell.img_post_thumbnail.center;


viewWantedToCenter.center = baseView.center;//The view on which you wanted to center  "viewWantedToCenter" view.

我希望这会有所帮助。

您可以像这样直接匹配两个图像视图的中心

    UIImageView *playIcon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,20,20)];
    playIcon.image=[UIImage imageNamed:@"play_icon.png"];

    playIcon.center = cell.img_post_thumbnail.center;
   [cell.contentView addSubview:playIcon];

我强烈建议使用自动布局:

[[cell img_post_thumbnail] addConstraint:
    [NSLayoutConstraint constraintWithItem:playIcon
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:[cell img_post_thumbnail]
                                 attribute:NSLayoutAttributeCenterX
                                multiplier:1.0
                                  constant:0.0]];

[[cell img_post_thumbnail] addConstraint:
    [NSLayoutConstraint constraintWithItem:playIcon
                                 attribute:NSLayoutAttributeCenterY
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:[cell img_post_thumbnail]
                                 attribute:NSLayoutAttributeCenterY
                                multiplier:1.0
                                  constant:0.0]];
编辑


哦,记住设置
[[cell img_post_缩略图]设置translates自动调整大小gmaskintoconstraints:NO]以编程方式创建视图时。

对于设置图像或单元格中心的任何内容,可以设置ImageView的位置,如:(cell.frame.size.with-UIImage.frame.size)/2请发布完整答案,我不明白为什么要使用self.view.center.x/y?它应该分别为centerImageView.x=cell.contentView.center.x。或者cell.frame.size.width/2表示X,height/2表示yI仍然不明白,为什么要在图像视图的中心点添加图像?您可以简单地创建有关图像的imageview,并使该imageview位于单元格的中心!!!你不需要计算两次中心点!!!!请给我以下NSLOG(@“cell.img_post_缩略图:%@”,NSStringFromcRect(cell.img_post_缩略图.frame));NSLog(@“cell.img_post_缩略图:%@”,NSStringFromCGPoint(cell.img_post_缩略图.center));NSLog(@“playIcon:%@”,NSStringFromCGRect(playIcon.frame));NSLog(@“playIcon:%@”,NSStringFromCGPoint(playIcon.center));
[[cell img_post_thumbnail] addConstraint:
    [NSLayoutConstraint constraintWithItem:playIcon
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:[cell img_post_thumbnail]
                                 attribute:NSLayoutAttributeCenterX
                                multiplier:1.0
                                  constant:0.0]];

[[cell img_post_thumbnail] addConstraint:
    [NSLayoutConstraint constraintWithItem:playIcon
                                 attribute:NSLayoutAttributeCenterY
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:[cell img_post_thumbnail]
                                 attribute:NSLayoutAttributeCenterY
                                multiplier:1.0
                                  constant:0.0]];