gpt4 book ai didi

cocoa-touch - 自动布局和 iOS7,以编程方式添加的 UIImageView 不会出现 - 似乎与线程相关

转载 作者:行者123 更新时间:2023-12-02 21:49:50 24 4
gpt4 key购买 nike

我有一个充满 UIImages 的数组。我将每个图像加载到 UIImageView 中,然后将 UIImageView 添加到 UIView 中,完成后,UIView 将添加到显示屏上的 UIView 中。

问题出在 iOS7 中,当我将带有 UIImageViews 的 UIView 添加到其他 UIView 时,它们不会出现在屏幕上(在 iOS6 中一切正常)。如果我按下主页按钮,然后返回应用程序,图像就在那里!

我知道这听起来有点奇怪,因为我可以从进程中删除 UIView,但这是有原因的。我不将 UIImages 作为 UIImageViews 存储在数组中也是有原因的。

UIView *layerViewToAdd = [[UIView alloc]initWithFrame:presentationDisplay.frame];

for(UIImage tempImage in currentImages)
{
UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:presentationDisplay.frame];
[tempImageView setImage:tempImage];
[layerViewToAdd addSubview:tempImageView];
}

[presentationDisplay addSubview:layerViewToAdd];

我尝试使用 [presentation setNeedsDisplay] 和 [presentationDisplay setNeedsLayout] 强制屏幕重绘,但不起作用。我认为问题与自动布局/约束有关,但不确定。

编辑

好的,我已经隔离了代码,它似乎与线程相关。看下面的代码,我在后台调用 redrawLayersInBackground 这是上面的 FOR 循环,然后我在主线程上调用 PresentLayersInView,它添加了已经显示的 View (上面的最后一行或多或少) - 抱歉我我知道我删掉了很多代码,但它的作用远不止于此(吸取的教训)。这在 iOS6 中工作正常,但在 iOS7 中不行,如果我取出所有调度位,它在两者中都工作正常。

是否有更好的方法在后台构建内容然后在前台添加?

backgoundQueue = dispatch_queue_create("createlayers", 0);

dispatch_async(backgoundQueue, ^{

UIView *layerViewToAdd = [self redrawLayersInBackground];

dispatch_async(dispatch_get_main_queue(), ^{
[self presentLayersInView:layerViewToAdd];

});

});

另一次编辑

这是整个代码;

它将 View 的图层存储在一个数组中,并且在再次下载之前进行一些比较和测试,看看我们是否已经拥有该图层。下来的图像很大,这就是为什么我遇到这么多麻烦!

-(UIView *)redrawLayersInBackground
{
int layerCount = 0;


// This is the view which will contain all the new layers
UIView *layerViewToAdd = [[UIView alloc]initWithFrame:presentationDisplay.frame];

// Take a copy of all the layers then clear then, the copy will be used as a cache to build a new set of layers
NSArray *tempLayerNames = [[NSArray alloc]initWithArray:layerNames];
NSArray *tempLayerImages = [[NSArray alloc]initWithArray:layerImages];

[layerNames removeAllObjects];
[layerImages removeAllObjects];


// This for is just to loop through all items, the dictionary is not used
for (NSDictionary *enforceLoop in currentSetupInfo[@"layers"])
{

NSString *layerName =currentSetupInfo[@"layers"][[NSString stringWithFormat:@"layer%i", layerCount]];

BOOL isFound = NO;
UIImage *tempImage;
UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:presentationDisplay.frame];

int newLayerCount = 0;

// Start a search for the layer in the list of old layers
for (NSString *obj in tempLayerNames)
{
if([obj isEqualToString:layerName])
{
// NSLog(@"Found Layer : %@", layerName);

[layerNames addObject:obj];
tempImage = [tempLayerImages objectAtIndex:newLayerCount];
[layerImages addObject:tempImage];

isFound = YES;
break;
}
newLayerCount++;
// This layer is already visiable ont he screen, do not fade it in
tempImageView.alpha = 1.0;
}

if (!isFound)
{
// NSLog(@"Downloading Layer : %@", layerName);

// NSURL *layerURL = [NSURL URLWithString:layerName];

// tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:layerURL]];

tempImage = [self imageFromURLInString:layerName];

[layerNames addObject:layerName];
if(tempImage == nil)
{
NSLog(@"Error - Image not fonud on server..........................................................");
NSLog(@"Error - URL : %@", layerName);
[layerImages addObject:[[UIImage alloc]init]];
}
else
{
// tempImageView.image = tempImage;
[layerImages addObject:tempImage];
}

// Set this layer Alpha to zero, this way we can fade it in
tempImageView.alpha = 1.0;
}

// NSLog(@"Image Layer : %i", layerCount);

[tempImageView setImage:tempImage];

[layerViewToAdd setTranslatesAutoresizingMaskIntoConstraints:YES];
[layerViewToAdd addSubview:tempImageView];


NSLog(@"Iterating");

layerCount++;

dispatch_async(dispatch_get_main_queue(), ^{

NSNumber *tempPercentage = [NSNumber numberWithFloat: (float) (layerCount) / [currentSetupInfo[@"layers"] count]];

[self updateSpinner:tempPercentage];
});
}

return layerViewToAdd;
}


-(void)presentLayersInView:(UIView *)layerViewToAdd
{
layerViewToAdd.alpha = 0.0f;

[presentationDisplay setTranslatesAutoresizingMaskIntoConstraints:YES];
[presentationDisplay addSubview:layerViewToAdd];


UIView *layerViewToRemove;


if(layerViewTag != 0)
{
layerViewToRemove = [presentationDisplay viewWithTag:layerViewTag];
[presentationDisplay sendSubviewToBack:layerViewToRemove];
// Swap the number used for the TAG (will toggle between 1 and -1)
layerViewTag = layerViewTag * -1;
layerViewToAdd.tag = layerViewTag;
}
else
{
// Fist time setup
layerViewTag = 1;
layerViewToAdd.tag = layerViewTag;
}

layerViewToRemove.alpha = 0.0;
layerViewToAdd.alpha = 1.0f;
presentationDisplay.alpha = 1.0;

[layerViewToRemove removeFromSuperview];
[self stopSpinner];
}

最佳答案

几天后我想通了......

所以layerNames和layerImages都是NSMutableArrays,但是,在代码的早期我已经设法注释掉它们的Alloc Init。

最终结果是,在 iOS6 中工作正常,但在 iOS7 中却出现奇怪的结果。将此部分添加回来,第一次运行良好。

对自己很生气......有点担心它能成功!

关于cocoa-touch - 自动布局和 iOS7,以编程方式添加的 UIImageView 不会出现 - 似乎与线程相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com