iOS中更换头像

w首先一般更换头像的图片从二个地方获取:相机或者相册

1.打开相机或者相册代码如下

UIImagePickerController*picker=[[UIImagePickerControlleralloc]init];

picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary(相机),UIImagePickerControllerSourceTypeCamera(相册);

picker.mediaTypes=[UIImagePickerControlleravailableMediaTypesForSourceType:picker.sourceType];

picker.allowsEditing=YES;

picker.delegate=self;

[selfpresentViewController:pickeranimated:YEScompletion:nil];

2.然后要遵守UIImagePickerControllerDelegate,UINavigationControllerDelegate

调用代理方法- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

里面创建个UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage(可编辑的)];也可以设置成不可编辑的

这样创建的image就是你从相机或者相册选定的图片了 

当然选取完图片之后要记得关闭视图

[selfdismissViewControllerAnimated:YEScompletion:nil];

xcode默认语言是英文,所以打开相机或相册提示的地方也显示为英文,在info.plist里面新增Localized resources can be mixed为YES就可以了。

你可能感兴趣的:(iOS中更换头像)