0

我一直在尝试创建一个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");



}

此外,当我在 iPhone 上运行此方法时,它会给我一个警告,即

CreatingVideo[334]:CGContextSaveGState:无效上下文 0x0。这是一个严重的错误。此应用程序或它使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体下降。此通知是出于礼貌:请解决此问题。这将成为即将到来的更新中的致命错误。

4

2 回答 2

1

你的代码没有意义。您有一个贯穿数组的 for 循环,将图像添加到图像视图中,从当前屏幕外上下文中捕获 2 个图像中的 1 个,然后在第一次通过数组后结束图像上下文。在第二次通过时,上下文不再存在,因此第二次结束上下文调用将失败。

将结束上下文移到 for 循环之外。那应该可以解决您的警告。

即使这样,我也不确定从图像视图中删除子视图、添加新视图、将内容渲染到图形上下文是否有效,所有这些都不会返回。UIKit 视图绘制通常将 UI 更改排队,直到您的代码返回,然后在下一次通过事件循环时呈现它。

于 2014-06-14T02:02:26.103 回答
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");



}
于 2014-06-14T03:16:47.533 回答