我一直在尝试创建for
循环,将UILabel.text
合并为UIImage
然后将其保存到实例中,然后进入for
}第二次循环它会将结果保存在不同的实例中。
我面临的问题是它正在保存第一个UIImage
,但质量低,并且跳过了第二个UIImage
。我甚至尝试在按钮操作中执行-(void)CreateTheImage{
UIImage * img = [UIImage imageNamed:@"SquareBackground.jpg"];
UIGraphicsBeginImageContext(_imgView.bounds.size);
int Counter = 0;
for (UIView * view in [_imgView subviews]){
[view removeFromSuperview];
}
NSString *txt1 = @"Test";
NSString *txt2 = @"Worked Well";
[array addObject:txt1];
[array addObject:txt2];
UILabel *lbl = [[UILabel alloc]init];
NSLog(@"%i",[array count]);
NSLog(@"Before the loop");
for (int x = 0; x < [array count]; x++) {
NSLog(@"Entered the loop");
NSLog(@"x = %i",x);
lbl.text = [array objectAtIndex:x];
NSLog(@"lbl.text = %@",[array objectAtIndex:x]);
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setBackgroundColor:[UIColor clearColor]];
lbl.font = [UIFont boldSystemFontOfSize:16];
[lbl setFrame:CGRectMake(20,112.5, 260, 75)];
[_imgView addSubview:lbl];
[_imgView bringSubviewToFront:lbl];
[_imgView setImage:img];
[_imgView.layer renderInContext:UIGraphicsGetCurrentContext()];
Counter = x+1;
NSLog(@"counter = %i",Counter);
switch (Counter) {
case 1:
NSLog(@"swintch case 1");
newImage1 = UIGraphicsGetImageFromCurrentImageContext();
break;
case 2:
NSLog(@"swintch case 2");
newImage2 = UIGraphicsGetImageFromCurrentImageContext();
break;
default:
break;
}
NSLog(@"after switch");
UIGraphicsEndImageContext();
}
//To save the picture in the album
UIImageWriteToSavedPhotosAlbum(newImage1, nil, nil, nil);
UIImageWriteToSavedPhotosAlbum(newImage2, nil, nil, nil);
NSLog(@"After the loop");
}
保存代码,但仍然无效。
{{1}}
当我在iPhone上运行此方法时,它会给我一个警告
CreatingVideo [334]:CGContextSaveGState:无效的上下文0x0。这是一个严重的错误。该应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降低。此通知是礼貌的:请解决此问题。在即将到来的更新中,它将成为一个致命的错误。
答案 0 :(得分:1)
您的代码没有意义。你有一个贯穿数组的for循环,将图像添加到图像视图,从当前的off-screnen上下文中捕获2个图像中的1个,然后在第一次通过数组后结束图像上下文。在第二次传递时,上下文不再存在,因此第二次结束上下文调用将失败。
将结束上下文移到for循环之外。这应该可以解决你的警告。
即便如此,我也不确定它是否能够从图像视图中删除子视图,添加新视图,将内容渲染到图形上下文,所有这些都无需返回。 UIKit视图绘图通常将更改排队到UI,直到您的代码返回,然后在下一次通过事件循环时呈现它。
答案 1 :(得分:0)
感谢Duncan C帮助我找到答案并更正代码以便现在正常工作
-(void)CreateTheImage{
UIImage * img = [UIImage imageNamed:@"SquareBackground.jpg"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 320), NO, 0.0f);
int Counter = 0;
NSString *txt1 = @"Test";
NSString *txt2 = @"Worked Well";
[array addObject:txt1];
[array addObject:txt2];
UILabel *lbl = [[UILabel alloc]init];
NSLog(@"%i",[array count]);
NSLog(@"Before the loop");
for (int x = 0; x < [array count]; x++) {
for (UIView * view in [_imgView subviews]){
[view removeFromSuperview];
}
NSLog(@"Entered the loop");
NSLog(@"x = %i",x);
lbl.text = [array objectAtIndex:x];
NSLog(@"lbl.text = %@",[array objectAtIndex:x]);
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setBackgroundColor:[UIColor clearColor]];
lbl.font = [UIFont boldSystemFontOfSize:25];
[lbl setFrame:CGRectMake(20,122.5, 280, 75)];
[_imgView addSubview:lbl];
[_imgView bringSubviewToFront:lbl];
[_imgView setImage:img];
[_imgView.layer renderInContext:UIGraphicsGetCurrentContext()];
Counter = x+1;
NSLog(@"counter = %i",Counter);
switch (Counter) {
case 1:
newImage1 = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"swintch case 1");
break;
case 2:
NSLog(@"swintch case 2");
newImage2 = UIGraphicsGetImageFromCurrentImageContext();
break;
default:
break;
}
NSLog(@"after switch");
}
UIGraphicsEndImageContext();
//To save the picture in the album
UIImageWriteToSavedPhotosAlbum(newImage1, nil, nil, nil);
UIImageWriteToSavedPhotosAlbum(newImage2, nil, nil, nil);
NSLog(@"After the loop");
}