【问题标题】:Drawing 2 UIImage merged with different UILabel绘图 2 UIImage 与不同的 UILabel 合并
【发布时间】:2014-06-14 01:48:30
【问题描述】:

我一直在尝试创建一个 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。这是一个严重的错误。此应用程序或它使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体下降。此通知是出于礼貌:请解决此问题。这将成为即将到来的更新中的致命错误。

【问题讨论】:

    标签: ios objective-c uiimage uigraphicscontext


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您在保存过程中完美地工作,但保存质量仍然不高
    • 您可能应该使用 UIGraphicsBeginImageContextWithOptions 并传入设备比例。在视网膜设备上,UIGraphicsBeginImageContext 方法会将您的图像下采样到非视网膜,它看起来不会那么好。
    【解决方案2】:

    感谢 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");
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 2020-01-30
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多