您的位置:首页 > 移动开发 > IOS开发

IOS 从系统图库中获取 图片 并设置为头像

2014-04-15 18:52 330 查看
总会遇到一些应用要拍照设置图片之类的的总结一个方法备用以后会继续拓展到多图

@interfaceAddCustomerViewController:UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImage*chosenImage;//做显示在self.view上的image
NSString*tempImagePath;//图片路径
UIButton*button_Photo;//点击从系统选图片
}
@end
//以上代理和参数都是必须有的




-(void)TakePhoto
{
UIActionSheet*actionSheet=[[UIActionSheetalloc]initWithTitle:@"您想如何获取照片?"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",nil];
[actionSheetshowInView:self.view];
}
-(void)actionSheet:(UIActionSheet*)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex
{
/*
NSLog(@"%ld",(long)buttonIndex);//2--->取消
UIImagePickerController*picker=[[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
if(buttonIndex==0)
{//拍照
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
}
elseif(buttonIndex==1)
{//图库
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
[selfpresentViewController:pickeranimated:YEScompletion:NULL];
*/
NSLog(@"%ld",(long)buttonIndex);//2--->取消

if(buttonIndex==0)
{//拍照
UIImagePickerController*picker=[[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:pickeranimated:YEScompletion:NULL];
}

}

#pragma-markUIImagePickerControllerdelegate
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info
{
chosenImage=[infoobjectForKey:@"UIImagePickerControllerOriginalImage"];
//删除缓存文件
if([[NSFileManagerdefaultManager]fileExistsAtPath:tempImagePath]){
NSLog(@"theimageisexist");
NSFileManager*defaultManager;

defaultManager=[NSFileManagerdefaultManager];
NSError*error=[[NSErroralloc]init];
[defaultManagerremoveItemAtPath:tempImagePatherror:&error];

}
[UIImagePNGRepresentation(chosenImage)writeToFile:[NSTemporaryDirectory()stringByAppendingPathComponent:@"temp_image.png"]atomically:YES];

BOOLfileExists=[[NSFileManagerdefaultManager]fileExistsAtPath:tempImagePath];

if(fileExists){
NSLog(@"success");
customer_Inform.image_Head=chosenImage;
[button_PhotosetImage:chosenImageforState:UIControlStateNormal];

}
[pickerdismissViewControllerAnimated:YEScompletion:NULL];

if(fileExists){
NSLog(@"success");
[SGInfoAlertshowInfo:@"照片添加成功!"
bgColor:[[UIColordarkGrayColor]CGColor]
inView:self.view
vertical:0.5];
}
else
{
[SGInfoAlertshowInfo:@"照片添加不成功!"
bgColor:[[UIColordarkGrayColor]CGColor]
inView:self.view
vertical:0.5];
}
////也能用
//[button_PhotosetImage:[infoobjectForKey:@"UIImagePickerControllerOriginalImage"]forState:UIControlStateNormal];
//[pickerdismissViewControllerAnimated:YEScompletion:NULL];
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐