app實現上傳圖片

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">通過點擊ImageView,調用系統相冊或者相機選擇照片。</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">調用UIImagePickerController,需要有</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">UIImagePickerControllerDelegate</span><span class="s2" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">,</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">UINavigationControllerDelegate兩個協議,sourceType中</span><span class="s1" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">UIImagePickerControllerSourceTypePhotoLibrary是相冊,</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">UIImagePickerControllerSourceTypeCamera是相機(真機纔可以測試)。</span>

1.打開相冊

        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
        controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
        controller.mediaTypes = mediaTypes;
        controller.delegate = self;
        [self presentViewController:controller
                           animated:YES
                         completion:^(void){
//                             DDLogInfo(@"Picker View Controller is presented");
                         }];
2.選擇圖片,可以在這使用第三方的RSKImageCropper,用來自定義選取圓形或方形的圖片

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    __weak VerificationViewController *weakSelf = self;
    [picker dismissViewControllerAnimated:YES
                               completion:^() {
                                   UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
                                   //取得選擇的圖片
                                   weakSelf.userPortrait = portraitImg;
                                   //上傳圖片
                                   [weakSelf updateUserPortait];
                               }];
}
/**這個方法記得寫,用來點擊取消時返回*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES
                               completion:^{
                               }];
}
3.圖片上傳,大部分會採用base64加密,參數封裝

NSData *data                = UIImageJPEGRepresentation(self.userPortrait, 0.5);
    NSString *bodystring        = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    NSData  *imagedata          = [bodystring dataUsingEncoding:NSUTF8StringEncoding];
    NSString  *imageData        = [[NSString alloc] initWithData:imagedata
                                                        encoding:NSUTF8StringEncoding];




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章