如何保存照片上贴有标签的照片?

时间:2013-03-07 22:32:01

标签: iphone ios objective-c ipad ios5

因此,我想要在拍摄时将照片叠加在照片上。这是我的代码。是否有更简单的方法在照片上叠加数据?

- (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]);
        }
    }];

}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

你的代码似乎没问题,相比我的代码工作正常,但尝试使用新的局部变量 获取屏幕截图时,如下所示:

UIImage *cameraImage2 = UIGraphicsGetImageFromCurrentImageContext();

然后保存cameraImage2。

还要确保self.tempGreen不是ClearColor。