-2
我想將日期和時間添加到使用UIImagePickerController
拍攝的圖像中。將日期和時間添加到通過UIImagePickerController捕獲的圖像
我有一個使用控制器拍攝照片的應用程序,雖然我知道日期存儲在元數據中,但我想知道它是否可以輕鬆合併到圖像中(很像您可以將數碼相機設置爲)之類的例子如下所示:
我想將日期和時間添加到使用UIImagePickerController
拍攝的圖像中。將日期和時間添加到通過UIImagePickerController捕獲的圖像
我有一個使用控制器拍攝照片的應用程序,雖然我知道日期存儲在元數據中,但我想知道它是否可以輕鬆合併到圖像中(很像您可以將數碼相機設置爲)之類的例子如下所示:
注意,當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,其中包含日期;最終的結果看起來是這樣的:
你想在圖像上顯示的日期或你想將圖像發送給其他與日期添加到它.. – BalaChandra
我寫我想「合併到jpg圖像」,即它成爲圖像的一部分。這是一個不好的問題嗎? – RGriffiths