2013-03-07 130 views
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]); 
     } 
    }]; 

}

回答

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; 
} 
0

您的代码看起来不错,但相比于矿山的正常工作,但尝试使用新的局部变量 时得到如下截图:

UIImage *cameraImage2 = UIGraphicsGetImageFromCurrentImageContext(); 

然后保存cameraImage2。

还要确保self.tempGreen不是ClearColor。