如何从我的应用程序中拍摄照片并使用它来显示使用UIImageView

时间:2014-06-02 06:04:52

标签: ios xcode image uiimageview

我对Xcode很新,我想从我的应用程序中拍照,并在UIImageView上显示该图片。请帮忙谢谢!

3 个答案:

答案 0 :(得分:2)

First add <UIImagePickerControllerDelegate> protocol in header filer,after this on button action put  code-                                                                                          

 self.pickerImageView = [[UIImagePickerController alloc] init];
 self.pickerImageView.delegate = self;
self.pickerImageView.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.navigationController presentViewController:self.pickerImageView animated:YES completion:nil];

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.pickerImageView dismissViewControllerAnimated:YES completion:nil];
    yourimageview.iamge=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.pickerImageView=nil;
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self.pickerImageView dismissViewControllerAnimated:YES completion:nil];
    self.pickerImageView=nil;
}

答案 1 :(得分:1)

答案 2 :(得分:0)

以下是实现所需任务的代码:

#pragma mark - Change Profile picture


- (IBAction)profileImageButtonAction:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: nil
                                                             delegate: self
                                                    cancelButtonTitle: @"Cancel"
                                               destructiveButtonTitle: nil
                                                    otherButtonTitles: @"Take a new photo", @"Choose from existing", nil];

    [actionSheet showInView:self.appDelegate.window];


}



#pragma mark - UIActionSheet Delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    fromPhotoLibaryOrCamera = YES;
    switch (buttonIndex) {
        case 0:
            [self takeNewPhotoFromCamera];
            break;
        case 1:
            [self choosePhotoFromExistingImages];
            break;
        default:
            break;
    }
}


#pragma mark - UIImagePicker Delegates

- (void)takeNewPhotoFromCamera
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        controller.allowsEditing = NO;
        controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
        controller.delegate = self;
        [self.navigationController presentViewController: controller animated: YES completion: nil];

    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                        message:@"Camera not available"
                                                       delegate:self
                                              cancelButtonTitle:@"Try Again"
                                              otherButtonTitles: nil];
        [alert show];

    }


}
//Method to upload image from image library
-(void)choosePhotoFromExistingImages
{
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        controller.allowsEditing = NO;
        controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
        controller.delegate = self;
        controller.isAccessibilityElement = YES;
        [self.navigationController presentViewController: controller animated: YES completion: nil];
    }
}



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

{

    UIImage *image=[info valueForKey: UIImagePickerControllerOriginalImage];


    NSLog(@"Image Size = %@",NSStringFromCGSize(image.size));

    //image = [image resizedImage:imgProfile.bounds.size interpolationQuality:kCGInterpolationDefault];

    image = [image resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:imgProfile.bounds.size interpolationQuality:kCGInterpolationDefault];

    NSLog(@"Image Size = %@",NSStringFromCGSize(image.size));

    imgData = UIImageJPEGRepresentation(image, 0.1);

    imgProfile.image=image;
    [imgProfile addBorder:3.0 andColor:[UIColor lightGrayColor]];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{

    [self.navigationController dismissViewControllerAnimated: YES completion: nil];

}

在图像视图后面留一个按钮,然后用按钮连接按钮的IBAction - (IBAction)profileImageButtonAction:(id)sender 其余的工作将完成。希望这可以帮助。谢谢和干杯。 :)