ios - removeFromSuperview UIImageView 不工作

标签 ios objective-c xcode uiimageview

我正在尝试使用以下代码从现有 View 中删除图像:

-(void) deleteImage:(int)imageID{

//[self.imageArray removeObjectAtIndex:imageID];
//remove the image from the screen
for (UIView* view in self.view.subviews) {
    if ([view isKindOfClass:[UIImageView class]] && [view tag] == imageID) {
        //could be a bug here with the re-Ordering of the array (could add a helper method to reset all the tags on screen when this is called
        NSLog(@"view %@", view);
        //[self.imageArray removeObjectAtIndex:imageID];
        [view removeFromSuperview];
    }
  }
}

NSLog 输出以下内容:
view <UIImageView: 0x18b8d7b0; frame = (0 0; 768 2016); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x18b8d890>>

我遇到的问题是它似乎正在获取 ImageView 但它没有删除它。

最佳答案

由于您正在尝试修改用户界面,因此您必须在主线程上进行。因此,请执行以下操作:

dispatch_async(dispatch_get_main_queue(), ^{
    [view removeFromSuperview];
});

关于ios - removeFromSuperview UIImageView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271485/

相关文章:

ios - 更改 UIImageView 的图像时使用 2-3 MB

ios - 在 iOS 中 React Native 更改状态栏文本颜色

ios - UIPickerView 更新速度慢

objective-c - @interface 声明和@property 声明之间的区别

iphone - UIView 中图像之间的精确间距,就像 UITabViewController 中一样

ios - 更新pod内部库

ios - SdWebImage 问题中的事件指示器

ios - 如果已登录 LinkedIn App,LinkedIn sdk iOS 会出错

iphone - 如何在 NSMutableArray 中存储枚举值

ios - 可达性返回错误状态