以编程方式在视图中添加图像

发布于 2024-12-15 00:04:24 字数 711 浏览 2 评论 0原文

我以编程方式在视图中添加了图像。但是当我切换到查看添加图像的位置时,我的应用程序已中止

UIImageView *countryFlag = [[UIImageView alloc] initWithImage:[UIImage imageNamed:CountryFlagImage]];
[countryFlag setFrame:CGRectMake(0, 0, 50, 50)];
[self.view addSubview:countryFlag];
[countryFlag release];
countryFlag = nil;

- (id)initWithNibName 我有

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil CountryFlagImage:(NSString*)imageFlagName
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) { 
         CountryFlagImage = [NSString stringWithString:imageFlagName];
    }
    return self;
}

从以前的主视图中获取的 CountryFlagImage。

I added image in my view programmatically. But my app has aborted when I switch to view where added image with code

UIImageView *countryFlag = [[UIImageView alloc] initWithImage:[UIImage imageNamed:CountryFlagImage]];
[countryFlag setFrame:CGRectMake(0, 0, 50, 50)];
[self.view addSubview:countryFlag];
[countryFlag release];
countryFlag = nil;

In - (id)initWithNibName I have

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil CountryFlagImage:(NSString*)imageFlagName
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) { 
         CountryFlagImage = [NSString stringWithString:imageFlagName];
    }
    return self;
}

CountryFlagImage I took from previous main view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-12-22 00:04:24

看起来像是保留问题...尝试保留 CountryFlagImage 也许在您可以使用它之前已被释放。

Looks like a retain problem... try retaining CountryFlagImage maybe is being released before you can use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文