1. 程式人生 > >iOS中新增圖片選擇器的實現

iOS中新增圖片選擇器的實現

//imageview新增點選事件的方法,搜尋tap gesture 為圖片新增,並在右側選擇interfaceenable,後為左側的tap gesture拖入事件

//下方法為點選圖片後從相簿插入圖片

- (IBAction)imageSelect:(id)sender {

if(![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

return;

    }else{

UIImagePickerController *pick=[[UIImagePickerController

alloc]init];

    pick.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

    pick.delegate=self;

        [selfpresentViewController:pick animated:YEScompletion:nil];}

}

//選擇圖片完畢的方法。讓選擇器消失,並將選中的圖片設定為控制元件的背景圖片

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

NSDictionary<NSString *,id> *)info{

//讓選擇器消失

    [picker dismissViewControllerAnimated:YEScompletion:nil];

//設定圖片

self.imagerr.image=info[UIImagePickerControllerOriginalImage ];

}