Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Uiview 从视图以编程方式获取UIImageView。_Uiview_Uiimageview - Fatal编程技术网

Uiview 从视图以编程方式获取UIImageView。

Uiview 从视图以编程方式获取UIImageView。,uiview,uiimageview,Uiview,Uiimageview,我正在代码中创建一个UIImageView,我想稍后获取它并设置一个图像。我该怎么做请帮忙 for (int x = 1; x <= 5; x++) { UIImageView *Game_ImageViews = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; Game_ImageViews.tag = x; [Game_View ad

我正在代码中创建一个UIImageView,我想稍后获取它并设置一个图像。我该怎么做请帮忙

for (int x = 1; x <= 5; x++) {
UIImageView *Game_ImageViews = [[UIImageView alloc] 
                            initWithFrame:CGRectMake(0, 0, 100, 100)];
    Game_ImageViews.tag = x;
    [Game_View addSubview:Game_ImageViews];
}


            UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath]      
                                                                  stringByAppendingPathComponent:@"Test.png"]];
        (UIImageView*)[self.view viewWithTag:100].image = image;
        [image release];

for(int x=1;x
viewWithTag
将为您提供带有标记的视图(如果存在带有该标记的子视图)。因此,例如,您的
self.view
必须具有标记为100的子视图

最初,您将使用标签1、2、3、4、5设置
Game_View
的子视图

因此,要按标记检索子视图,您必须执行以下操作:假设您要检索按id
1
标记的视图:

 ((UIImageView*)[Game_View viewWithTag:1]).image = image;
还有几件值得一提的事情。您没有在上面的循环中发布
Game\u imageview
,因此会导致内存泄漏


此外,如果您使用
UIImage
imageNamed
方法,这将给您带来两个好处。首先,您将拥有一个可以提高性能的缓存映像。其次,您将拥有一个自动释放的对象,从而省去您自己释放它的麻烦。当然,除非您希望通过do获得最大的性能和控制正如您在上面所做的那样,我正在重新设置它。

非常感谢。事实上,我知道我的代码中的标签号不正确,我只是发布了错误的标签号,但我没有发现我试图从错误的视图中获取图像。Duh。使用点概念与您不一样,我想我已经在那里了,因为它需要转换为UIImageView。必须是writ如下所示:[(UIImageView*)[Game_View WithTag:1]setImage:image];是的,关于语法您是对的。我添加了一些括号来解决这个问题。您需要将viewWithTag返回的UIView强制转换为UIImageView,然后调用该视图的.image属性。