从ipad相机相册读取相片并保存

以下是从实际项目中截取的例子,从一个button中启动获得相片

-(IBAction)blumbtnTap:(id)sender
{
    // 判断是否支持相机
//    UIAlertView *alertview;
//    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
//    {
//        alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"相册选择",@"相机拍照", nil];
//        alertview.tag=859;
//        [alertview show];
//        [alertview release];
//    }
//    else {
//        
//        alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"相册选择", nil];
//        alertview.tag=859;
//        [alertview show];
//        [alertview release];
//    }
    UIActionSheet *sheet;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
    }
    else {
        
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
    
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {//取消
        
    }
    else if (buttonIndex==1)//相册选择
    {
        
    }
    else if (buttonIndex==2)//相机拍照
    {

    }
    NSLog(@"%d",buttonIndex);
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            switch (buttonIndex) {
                case 0:
                    // 取消
                    return;
                case 1:
                    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                    
                case 2:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }
        else {
            if (buttonIndex == 0) {                
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面 
        UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypePhotoLibrary]) {
            m_imagePicker.sourceType = sourceType; //UIImagePickerControllerSourceTypePhotoLibrary;
            m_imagePicker.delegate = self;
            [m_imagePicker setAllowsEditing:NO];
            UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
            popoverController = popover; 
            [popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.
             view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
           
        }else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!"
                                                          delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
            [alert show];  
            [alert release];  
        }
    }
}

#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}



#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    ColorData *acolor=[GoodslistDataCls.colorList objectAtIndex:(currentSelectTag-DETAILDISHINFOVIEWSTARTTAG)];
    NSString *imgName=[NSString stringWithFormat:@"%@_%@.png",GoodslistDataCls.goodInfo.goodscode,acolor.colorcode];
    [self saveImage:image withName:imgName];
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imgName];
    
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    
    isFullScreen = NO;
    [imgView setImage:savedImage];
    imgView.tag = 100;
    
    [popoverController dismissPopoverAnimated:YES];
    
    DishInfoView *aDishInfoView=(DishInfoView *)[self.view viewWithTag:(currentSelectTag)];
    aDishInfoView.lblName.textColor=[UIColor redColor];
    aDishInfoView.imgView.image=[self readImagewithName:imgName];
    
    [delegate loadDishInfoImg:self.myTag];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}
原文地址:https://www.cnblogs.com/nonato/p/Nonato.html