以编程方式添加重复的 uiview

发布于 2024-12-25 07:14:07 字数 558 浏览 5 评论 0原文

我目前正在制作一个照片装饰应用程序。 *holderView 是用户选择的贴纸。每当我尝试从照片库加载照片或拍摄照片然后加载回此页面时,都会以编程方式添加额外的 *holderView,这是我之前在拍照之前选择的贴纸,之后会出现重复的贴纸,其中这不是我想要的。

我应该如何编写代码才能不让这种情况发生? 多谢。

- (void)viewWillAppear:(BOOL)animated {

UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
...
}

I'm currently making a photo decoration app. The *holderView is the sticker that has been chosen by user. Whenever I try to load a photo from photo library or take a photo and then load back to this page, additional *holderView added programmatically, which is the sticker that I've previously chosen before taking a photo, duplicated sticker appears after that, which is not what I want it to be.

How should I write the code for not letting this happen?
Thanks a lot.

- (void)viewWillAppear:(BOOL)animated {

UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
...
}

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

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

发布评论

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

评论(1

淡写薰衣草的香 2025-01-01 07:14:07

您的问题似乎是您使用 viewWillAppear 方法而不是 viewDidLoad 。这将导致多个“imageView”,因为每次隐藏时都会添加一个新的“imageView”,然后显示它所在的 viewController。您要做的是将 imageView 的创建(如果真的假设只有 1 个)移动到viewDidLoad 方法并使该 imageView 可被整个类访问,然后在 viewWillApear 中只需将 imageView 内的图像更改为新选择的图像即可。

Your problem seems to be that your using the viewWillAppear method instead of the viewDidLoad. This will cause multiple "imageViews" because your adding a new one every time you hide then show the viewController it's presented in. what you want to do is move the creation of the imageView (if there really is suppose to only be 1) to the viewDidLoad method and make that imageView accessible to the entire class, then in the viewWillApear simply change the image inside the imageView to the newly selected one.

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