我想使用UIImagePickerController
添加日期和时间。
我有一个使用控制器拍照的应用程序,虽然我知道日期存储在元数据中,但我想知道它是否可以轻松地合并到图像中(就像你可以设置数码相机一样)如下例所示:
答案 0 :(得分:0)
请注意,这仅适用于导入和包含ImageIO框架的情况。另一个条件是:
[UIImagePickerControllerMediaMetadata]仅在使用源类型设置为UIImagePickerControllerSourceTypeCamera的图像选择器时有效,并且仅适用于静止图像。
这是我们从图片中检索日期的方式。如果您允许通过相册选择图像,则此解决方案将无法使用。为此,您需要利用ALAsset
。如果你纯粹使用相机(并在iOS7上运行),这个解决方案应该可行。
#import <ImageIO/ImageIO.h>
// Ensure you've set yourself as the UIImagePickerController delegate to ensure this method is called
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* image = info[UIImagePickerControllerOriginalImage];
image = [self imageWithRenderedDateMetadata:info[UIImagePickerControllerMediaMetadata]
onImage:image];
}
- (UIImage*)imageWithRenderedDateMetadata:(NSDictionary*)metadata onImage:(UIImage*)image
{
if (!image || !metadata) { return nil; }
// Get Date String
// Note: You can format the date string here into a more readable format if you want
// Here, I'll just use the YYYY:MM:DD HH:MM:SS format given
NSDictionary *tiffMetadata = metadata[(__bridge id)kCGImagePropertyTIFFDictionary];
NSString* dateString = tiffMetaData[(__bridge id)kCGImagePropertyTIFFDateTime];
UIGraphicsBeginImageContext(image.size);
// Draw the image into the context
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// Position the date in the bottom right
NSDictionary* attributes = @{NSFontAttributeName :[UIFont boldSystemFontOfSize:30],
NSStrokeColorAttributeName : [UIColor blackColor],
NSForegroundColorAttributeName : [UIColor yellowColor],
NSStrokeWidthAttributeName : @-2.0};
const CGFloat dateWidth = [dateString sizeWithAttributes:attributes].width;
const CGFloat dateHeight = [dateString sizeWithAttributes:attributes].height;
const CGFloat datePadding = 25;
[dateString drawAtPoint:CGPointMake(image.size.width - dateWidth - datePadding, image.size.height - dateHeight - datePadding)
withAttributes:attributes];
// Get the final image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
你现在有一个UIImage,其中包含日期;最终结果看起来像这样: