Objective c Can';以编程方式在UIImageView中居中显示图像
在UIView子类init method中,我有以下内容: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
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
嗨,你能分享你的答案吗?