Take photo with iPhone and then use it!
I have an app that takes a photo and puts it in an image view. Simple. Code looks like this:
- (void)takePhoto:(id)sender
{
// Lazily allocate image picker controller
if (!imagePickerController) {
imagePickerController = [[UIImagePic开发者_如何学GokerController alloc] init];
// If our device has a camera, we want to take a picture, otherwise, we just pick from
// photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}else
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
}
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
image = [ImageHelpers imageWithImage:image scaledToSize:CGSizeMake(480, 640)];
[imageView setImage:image];
[self dismissModalViewControllerAnimated:YES];
}
When I use the Camera Roll everything works great, but if I use the actual Camera the image view is just black. Why is that?
Do I need to save it to the camera roll before I use it in the image view?
Ok. Found the solution myself.
I had to dismiss the modal view first...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES]; //Do this first!!
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
image = [ImageHelpers imageWithImage:image scaledToSize:CGSizeMake(480, 640)];
[imageView setImage:image];
}
Why you can't just use that code?
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[imageView setImage:image];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
精彩评论