0

私のアプリでは、次のことを行いたい: a.) ユーザーにカメラを提示して写真を撮る b.) メールを作成し、撮影した写真を添付する

写真の撮り方が分かりました。しかし、メールに添付する方法がわかりません。私が見た例では、ファイル名はわかっています。これは一例です。

picker.mailComposeDelegate = self;
     [picker setSubject:@"I have a pencil for you"];

     UIImage *roboPic = [UIImage imageNamed:@"RobotWithPencil.jpg"];
     NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
     [picker addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"RobotWithPencil.jpg"];

     NSString *emailBody = @"This is a cool image of a robot I found.  Check it out!";
     [picker setMessageBody:emailBody isHTML:YES];

     [self presentModalViewController:picker animated:YES];

コールバック メソッドから画像を取得していますが、カメラ ロールに保存されます。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [picker dismissModalViewControllerAnimated:TRUE];
    [picker release];
     }

しかし、そこから、画像をメールに添付する方法がわかりません。主な理由は、UIImagePickerController カメラ ソースを使用して取得するときに、写真がカメラ ロールに追加されたときのファイル名がわからないからですか?

したがって、次のいずれかが必要だと思います: a.) 保存した画像ファイルの名前を取得する方法を見つける、または b.) UIImage データを電子メールの添付ファイルとして添付する別の方法。

どんな助けでも感謝します。

4

2 に答える 2

0

以下のコードを使用します

    -(void)yourmethodname_clicked{
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
                picker.delegate = self;
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
                 picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                 [self presentModalViewController:picker animated:YES];
        }
        else{
                 UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not            Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

                 [altnot show];
                 [altnot release];

        }
    }


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];


MFMailComposeViewController *mailpicker = [[MFMailComposeViewController alloc] init];

    [mailpicker.navigationBar setTintColor:[UIColor blackColor]];
    mailpicker.mailComposeDelegate = self;

    [mailpicker setSubject:@"set your subject"]; 

    NSArray *toRecipients = [NSArray arrayWithObjects: nil];

    [mailpicker setToRecipients:toRecipients];

    UIImage *picture = [info objectForKey:@"UIImagePickerControllerOriginalImage"]];;

    NSData *imageData = UIImagePNGRepresentation(picture);
    [mailpicker addAttachmentData:imageData mimeType:@"image/png" fileName:@"set your file name"];

    NSString *emailBody = @"set your body string";
    [mailpicker setMessageBody:emailBody isHTML:YES];
    [mailpicker setSubject:@"set your subject"];
    mailpicker.title = @"Email";

    [self presentModalViewController:mailpicker animated:YES];
    [mailpicker release];
}

これはあなたを助けるかもしれません

于 2012-04-21T09:56:25.450 に答える
0

UIに関して何をしようとしているのか理解できないかもしれませんが、コードが提供するものからこれが機能しないのはなぜですか:

編集: (デリゲートを使用するようにコードを変更)

- (void)imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info {


// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if([delegate respondsToSelector:@selector(didSelectImage:)])
     [delegate didSelectImage:image];
 }

これは、ヘッダー ファイル (通常はクラス宣言の上) に入ります。

@protocol PostHelperDelegate <NSObject>

@optional
- (void)DimissModal;
//image data for image selected
- (void)DidSelectImage:(UIImage*)image;
@end

また、クラスでこのプロパティを作成する必要があります

@property(nonatomic,assign)id<PostHelperDelegate>* delegate; //don't forgot to synthizie

次に、デリゲートを実装するクラスで、デリゲートをクラスに割り当てます。

    Poster = [[delegateclass alloc] init];
    Poster.delegate = self;

次に、デリゲート関数を追加します。

 - (void)DidSelectImage:(UIImage*)image
 {
   //handle image here
 }

これをかなり単純化します。動作に問題がある場合はお知らせください

http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/

更新前:

個人的には、デリゲートを使用して画像が選択されたことを通知し、メール ピッカーを起動しますが、それは別のことです。UIImage に関しては、意味のある名前が付けられているとは思わないため、UIImagePicker に名前を付ける/または名前を取得する方法がわからないか、見つけたことがありません。デリゲートまたは詳細な説明に関する質問は、私に知らせてください。

于 2012-04-21T01:54:07.923 に答える