我如何使用UIImagePickerController将图像添加到推文?

时间:2012-01-24 17:52:00

标签: iphone ios xcode twitter uiimagepickercontroller

我是iOS开发的新手,所以我很困惑。我有一个ViewController,在这个ViewController中有很多UIButtons。如何打开选择器并使用addImage将图像添加到推文中? PS:我正在使用Storyboard。

谢谢,对不起我的英语......:)

1 个答案:

答案 0 :(得分:5)

按下按钮时,您需要执行以下操作:

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];        

    imagePicker.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;  

    imagePicker.delegate = self;

    [self presentModalViewController:imagePicker animated:YES];
  }

您需要实现委托方法。主要是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    NSData *imageData = UIImagePNGRepresentation(image);

    // do twitter stuff here....

    [self dismissModalViewControllerAnimated:YES];
}

有关更多信息,请参阅文档: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/PickinganItemfromthePhotoLibrary.html#//apple_ref/doc/uid/TP40010408-SW1