Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Can';以编程方式在UIImageView中居中显示图像_Objective C_Uiimageview_Ios5 - Fatal编程技术网

Objective c Can';以编程方式在UIImageView中居中显示图像

Objective c Can';以编程方式在UIImageView中居中显示图像,objective-c,uiimageview,ios5,Objective C,Uiimageview,Ios5,在UIView子类init method中,我有以下内容: imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 436)]; imageView.contentMode = UIViewContentModeCenter; [self addSubview:imageView]; UIImage* myImage = [UIImage imageWithDat

在UIView子类init method中,我有以下内容:

      imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 436)]; 
      imageView.contentMode = UIViewContentModeCenter;
      [self addSubview:imageView];
      UIImage* myImage = [UIImage imageWithData: 
                        [NSData dataWithContentsOfURL: 
                         [NSURL URLWithString: imageSource]]];
      myImage = [[MyMath sharedMySingleton] imageWithImage:myImage convertToSize:CGSizeMake(320, 436)]; // resizes image to set bounds
      imageView.contentMode = UIViewContentModeCenter;
      [imageView setImage:myImage];
imageView.contentMode=UIViewContentModeCenter应该居中显示图像,但它仍然显示在左上角。我做错了什么


编辑:对不起,我自己找到答案了。感谢大家

这是因为您的imageView大小和myImage大小相同。如果将myImage大小设置为小于imageView大小,则可以确定差异。

您的myImage大小等于imageView的大小。若要查看差异,请使myImage的大小略小于imageView的大小。之后,它将居中。在一行中更改图像的大小:

      myImage = [[MyMath sharedMySingleton] imageWithImage:myImage convertToSize:CGSizeMake(200, 200)]; // resizes image to set bounds

嗨,你能分享你的答案吗?