调用系统图片库选择图片,调用摄像头拍摄图片保存图片,调用摄像头录像保存录像视频片段

头文件:

包含 UIImagePickerControllerDelegate 协议

m文件:

选择图片接口:

-(void)ChoosePicture
{
    UIImagePickerController *Videopicker = [[UIImagePickerController alloc] init];
    Videopicker.delegate = self;
    [Videopicker setEditing:NO];
   
    Videopicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    Videopicker.allowsEditing = YES;
    isCamera = 0;

    
    [self.navigationController presentModalViewController:Videopicker animated:YES];
    
}

拍摄图片接口:

-(void)ShootPicture
{    
    
    UIImagePickerController *Videopicker = [[UIImagePickerController alloc] init];
    Videopicker.delegate = self;
    [Videopicker setEditing:NO];

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES
        ) {
        Videopicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        isCamera = 1;
    }
    else {
        Videopicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        Videopicker.allowsEditing = YES;
        isCamera = 0;
       }


    [self.navigationController presentModalViewController:Videopicker animated:YES];
    
}


录制视频接口:

-(void)ShootVideo
{    
    
    UIImagePickerController *Videopicker = [[UIImagePickerController alloc] init];
    Videopicker.delegate = self;
    [Videopicker setEditing:NO];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES
        ) {
        Videopicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        isCamera = 1;
    }
    else {
        Videopicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        Videopicker.allowsEditing = YES;
        isCamera = 0;
    }
    
    
    if (1) {
        NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:Videopicker.sourceType]; // mediaTypes用来确定再picker里显示那些类
型的多媒体文件,图片?视频?
        for (NSString *str in sourceTypes) {
            if ([str hasSuffix:@"movie"]) {
                Videopicker.mediaTypes = [[NSArray alloc] initWithObjects: str, nil];
                break;
            }
        }
    } 
    [self.navigationController presentModalViewController:Videopicker animated:YES];
    
}


实现协议:::::


共有三个可选的代理方法
– imagePickerController:didFinishPickingMediaWithInfo: 
– imagePickerControllerDidCancel: 
– imagePickerController:didFinishPickingImage:editingInfo:   Deprecated in iPhone OS 3.0

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
info中包括选取的照片,视频的主要信息
NSString *const UIImagePickerControllerMediaType;         选取的类型 public.image  public.movie
NSString *const UIImagePickerControllerOriginalImage;    修改前的UIImage object.
NSString *const UIImagePickerControllerEditedImage;      修改后的UIImage object.
NSString *const UIImagePickerControllerCropRect;           原始图片的尺寸NSValue object containing a CGRect data type
NSString *const UIImagePickerControllerMediaURL;          视频在文件系统中 的 NSURL地址




Deprecated in iPhone OS 3.0   3.0版本后就过时(老接口)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
	if (isCamera == 0) {
		UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); // 
	}
    
	NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
	[myFormatter setDateFormat:@"yyyyMMddhhmmss"];
	NSString *strTime = [myFormatter stringFromDate:[NSDate date]] ;
	[myFormatter release];
	NSString *strName = [strTime stringByAppendingString:@".png"];
//	NSString *strPath = [strDocPath stringByAppendingPathComponent:strName];
	NSData *imageData = UIImagePNGRepresentation(image);
	[imageData writeToFile: getFullPath(strName) atomically:YES];
	
	[picker dismissModalViewControllerAnimated:YES];
	[picker release];
}


通过摄像头拍摄的照片或视频后点击使用的处理以及从媒体库中选择照片后的处理


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	NSString *strType = [info valueForKey:UIImagePickerControllerMediaType];
    NSString *strSavedPath = nil;
    NSString *strName =nil;
	if ([strType hasSuffix:@"image"]) {
		UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

		if (isCamera == 1) {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);  // 保存拍摄的图片到媒体库(照片库)中
            //UIImage *orImage = nil;
            //UIImageWriteToSavedPhotosAlbum(orImage, nil, nil, nil);  
            //image = [orImage scaleToSize:CGSizeMake(orImage.size.width,orImage.size.height)];
		}
		NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
		[myFormatter setDateFormat:@"yyyyMMddhhmmss"];
		NSString *strTime = [myFormatter stringFromDate:[NSDate date]] ;
		[myFormatter release];
		strName = [strTime stringByAppendingString:@".png"];
		strSavedPath = getFullPath(strName);
		NSData *imageData = UIImagePNGRepresentation(image);
		[imageData writeToFile:strSavedPath atomically:YES];
        
        //BUGBUG
//    FileSeparationandUpload *testFilewrite=[FileSeparationandUpload shareUploadFile];  
//    testFilewrite.delegate=self;

        
        
//        FileSeparationandUpload *uploader = [[FileSeparationandUpload alloc]init];
//        [uploader UploadFile:strName];
//		NSArray *array = [NSArray arrayWithObjects:strType,strName,strPath,nil];
//		[fileArray addObject:array];
	}
	else {
		NSURL *mediaUrl = [info valueForKey:UIImagePickerControllerMediaURL];
        if (mediaUrl != nil) {
            BOOL filestatus = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([mediaUrl path]); // 判断该视频能否存储在媒体库(照片库)中
            if (filestatus)
            {
                UISaveVideoAtPathToSavedPhotosAlbum([mediaUrl path ], nil  , nil, nil); // 保存录制的视频片段到媒体库中(照片库)
            }
            NSFileManager *fileManger = [NSFileManager defaultManager];
            NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
            [myFormatter setDateFormat:@"yyyyMMddhhmmss"];
            NSString *strTime = [myFormatter stringFromDate:[NSDate date]] ;
            [myFormatter release];
            strName = [strTime stringByAppendingString:@".MOV"];
            strSavedPath = getFullPath(strName);
            NSURL *url = [NSURL fileURLWithPath:strSavedPath];
            NSError *error;
            [fileManger copyItemAtURL:mediaUrl toURL:url error:&error];
			
//            NSArray *array1 = [NSArray arrayWithObjects:strType,strName,strPath,nil];
//            [fileArray addObject:array1];
        }
	}
    [uploadManager AddFile:strName];
	[picker dismissModalViewControllerAnimated:YES];
	[picker release];
    [_tableView reloadData];

//    //BUGBUG
//    FileSeparationandUpload *uploader = [[FileSeparationandUpload alloc]init];
//    [uploader UploadFile:strSavedPath];
//	[myTableView reloadData];
}


取消选择图片或取消拍摄的处理


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	[picker dismissModalViewControllerAnimated:YES];
	[picker release];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值