UIImagePickerController将文本添加到图像

时间:2014-01-17 13:18:35

标签: ios objective-c cocoa uiimagepickercontroller

我正在尝试使用iOS中的相机拍摄图像或视频,并将一些文字添加到覆盖在图像底部的黑条中。

我正在尝试从用户输入中获取文本,并使用图像底部的文本和矩形保存图像:

enter image description here

我该怎么做?

我目前有这个:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    // A photo was taken/selected!
    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        // Save the image!
         UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
    }
}
else {
    // A video was taken/selected!
    self.videoFilePath = (__bridge NSString *)([[info objectForKey:UIImagePickerControllerMediaURL] path]);
    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        // Save the video!
         if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)) {
             UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
         }
    }
}

[self dismissViewControllerAnimated:YES completion:nil]; }

最好的解决方法是什么?

2 个答案:

答案 0 :(得分:0)

您可以在UIImageView上添加UIView,黑色(alpha为0.5)背景,在这个UIView上你可以添加带文字的UILabel。

答案 1 :(得分:0)

我想让用户使用UIAlertView进行输入

这是一个样本

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Title Here"
                      message:@"message"
                      delegate:self
                      cancelButtonTitle:@"Cancel" // or nil
                      otherButtonTitles:@"Ok",
                      nil];

alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];

表示会出现一个警告,其中包含一个文本框。由于我们将self设置为委托,因此您必须将该委托称为

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:@"Ok"]) {
    NSString *alertText = [alertView textFieldAtIndex:0].text;
    // create UILabel here and set the text as advised in answer above
    // example with global UILabel *overlayLabel;
    self.overlayLabel.text = alertText;
    // self.overlayLabel.font = [UIFont ...];
}
}

这基本上就是苹果所做的。转到音乐应用,点击“新播放列表”,您将看到它的实际效果

希望这有帮助!