0

所以我有数据要在拍摄时叠加在照片上。这是我的代码。有没有更简单的方法可以在照片上叠加数据?

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{         
    UIImage *cameraImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageView *imageViewToSave = [[UIImageView alloc] initWithImage:cameraImage];
    CGRect textFrame = CGRectMake(0, 0, imageViewToSave.frame.size.width-20, 325);
    UILabel *tempLabel = [[UILabel alloc] initWithFrame:textFrame];
    tempLabel.text = self.temperatureText.text;
    tempLabel.font = self.imageLabelFont;
    tempLabel.adjustsFontSizeToFitWidth = YES;
    tempLabel.textAlignment = NSTextAlignmentRight;
    tempLabel.textColor = self.tempGreen;
    tempLabel.backgroundColor = [UIColor clearColor];
    [imageViewToSave addSubview:tempLabel];

    UIGraphicsBeginImageContext(imageViewToSave.bounds.size);
    [imageViewToSave.layer renderInContext:UIGraphicsGetCurrentContext()];

    cameraImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self.library saveImage:cameraImage toAlbum:@"Node Therma" withCompletionBlock:^(NSError *error) 
    {
        if (error!=nil) 
        {
            NSLog(@"Big error: %@", [error description]);
        }
    }];

}

4

2 回答 2

1

刚刚测试了这个,它可以工作......

- (UIImage *)imageWithBackground:(UIImage *)background text:(NSString *)text textColor:(UIColor *)textColor {

    UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
    UIView *composition = [[UIView alloc] initWithFrame:imageView.bounds];
    [composition addSubview:imageView];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/* where you want the label */)];
    label.text = text;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = textColor;
    [composition addSubview:label];

    UIGraphicsBeginImageContext(composition.bounds.size);
    [composition.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *composedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return composedImage;
}
于 2013-03-07T23:05:19.977 回答
0

与我的代码相比,您的代码看起来不错,但在获取屏幕截图时尝试使用新的局部变量,如下所示:

UIImage *cameraImage2 = UIGraphicsGetImageFromCurrentImageContext();

然后保存cameraImage2。

还要确保 self.tempGreen 不是 ClearColor。

于 2013-03-07T23:08:57.410 回答