【发布时间】:2015-01-30 07:04:47
【问题描述】:
我想在图片上加上文字。为此,我编写了一个创建 UIView 的方法,其中 UIImageView 和 UITextView 作为其子视图。我从 UIView 的图形上下文中获取 UIImage。如果我将图像保存到相机胶卷,这可以正常工作。但是如果我将图像转换为 NSData 并将其上传到服务器,然后下载 NSData 并将其更改回 UIImage,则图像是空白的。
+(UIImage*)imageWithImage:(UIImageView *)sourceImageView addTextFromTextView:(UITextView *)textView {
UIView *parentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[parentView addSubview:sourceImageView];
[parentView addSubview:textView];
[parentView sizeToFit];
UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] drawInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
下面的代码可以正常工作,并且作为相机胶卷的正确图像
UIImageWriteToSavedPhotosAlbum([UIImage imageWithImage:self.imageView addTextFromTextView:self.textView], nil, nil, nil);
但是,此代码将空白图像保存到相机胶卷
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithImage:self.imageView addTextFromTextView:self.textView], 1.0);
UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:imageData], nil, nil, nil);
提前感谢您的帮助。
【问题讨论】:
标签: ios objective-c xcode uiimage uitextview