Cocoa touch Autolayout和iOS7,通过编程方式添加UIImageViews don';似乎与线程相关
我有一个满是图像的数组。我将每个图像加载到UIImageView中,然后将UIImageView添加到UIView中,一旦完成,UIView将添加到显示屏上的UIView中 问题是在iOS7中,当我将带有UIImageView的UIView添加到另一个UIView时,它们不会出现在屏幕上(在iOS6中都很好)。如果我按下Home(主页)按钮,然后返回应用程序,图像就会出现 我知道这听起来有点奇怪,因为我可以从过程中删除UIView,但这是有原因的。我不将UIImages作为UIImageView存储在数组中也是有原因的Cocoa touch Autolayout和iOS7,通过编程方式添加UIImageViews don';似乎与线程相关,cocoa-touch,ios7,uiimageview,constraints,autolayout,Cocoa Touch,Ios7,Uiimageview,Constraints,Autolayout,我有一个满是图像的数组。我将每个图像加载到UIImageView中,然后将UIImageView添加到UIView中,一旦完成,UIView将添加到显示屏上的UIView中 问题是在iOS7中,当我将带有UIImageView的UIView添加到另一个UIView时,它们不会出现在屏幕上(在iOS6中都很好)。如果我按下Home(主页)按钮,然后返回应用程序,图像就会出现 我知道这听起来有点奇怪,因为我可以从过程中删除UIView,但这是有原因的。我不将UIImages作为UIImageView
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]和[presentation Display setNeedsLayout]强制屏幕重画,但不起作用。我认为问题与自动布局/约束有关,但不确定
编辑
好的,我已经隔离了代码,它似乎与线程相关。查看下面的代码,我在背景中调用redrawLayersInBackground,这是上面的FOR循环,然后我在主线程上调用presentLayersInView,它将视图添加到已经显示的视图中(上面多多少少的最后一行)-抱歉,我知道我删掉了很多代码,但它所做的远不止此(经验教训). 这在iOS6中可以正常工作,但在iOS7中不行,如果我去掉所有的分派位,它在这两种情况下都可以正常工作
有没有更好的方法在后台构建东西,然后在前台添加
backgoundQueue = dispatch_queue_create("createlayers", 0);
dispatch_async(backgoundQueue, ^{
UIView *layerViewToAdd = [self redrawLayersInBackground];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentLayersInView:layerViewToAdd];
});
});
另一次编辑
这就是全部代码
它将视图的各个层存储在一个数组中,并进行一些比较和测试,以查看在再次下载之前是否已经有了该层。下来的图像很大,这就是为什么我要经历这么多的麻烦
-(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和LayerImage都是NSMutableArray,然而,在代码的早期,我已经设法注释掉了它们的Alloc Init 最终的结果是,在iOS6中运行良好,在iOS7中出现奇怪的结果。重新添加此部件,第一次运行良好
我对自己很生气。。。。。。。。。尽管如此,我还是有点担心它能成功 你试过关闭自动布局只是为了看看会发生什么吗?@hw731刚刚试过,但仍然不起作用。还有一堆我的其他动画也不起作用。我想知道我是否能够像在循环中那样分配UIImageView并添加为子视图!您确定正在迭代
for
循环吗?我会在它里面设置一个断点,看看它是否像你期望的那样循环。我在里面放了一个NSLOG,它至少循环了10次…@hw731当我添加子视图时,我创建的子视图的帧大小与主UIView相同,我应该设置一个约束吗?我觉得这些限制并没有产生(这就是为什么当应用程序被最小化和恢复时它们会起作用)!