上传图片的方法

1.要实现这两个代理 <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

#pragma mark - actionSheet delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    //给相机设置代理
    imagePickerController.delegate = self;
    //应许编辑
    imagePickerController.allowsEditing = YES;
    
    if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    }
    
    switch (buttonIndex) {
        case 0:
        {
            //相机
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            }
            else
            {
                imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
            break;
        }
        case 1:
        {
            //相册
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            {
                imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
            else
            {
                [Common showAlert:@"获取相册失败"];
            }
            break;
        }
        case 2:
        {
            //取消
            return;
            break;
        }
        default:
            break;
    }
    
    //模态跳转
    [self presentViewController:imagePickerController animated:YES completion:^{
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - UIImagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *fileImg = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    //把拍下来的照片保存到相册图库里
    UIImageWriteToSavedPhotosAlbum(fileImg, self, nil, nil);
    NSString *imgName = [self creatTimeData];
    [self saveImage:fileImg withName:imgName];
    
    //获取沙河路径(就是照片存的路径)
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imgName];
    
    UIButton *button = (UIButton *)[self.view viewWithTag:self.uploadingType + 10];
    if (self.uploadingType == BXUploadingPhoteTypeRepiar)
    {
        [button setImage:[UIImage imageWithContentsOfFile:fullPath] forState:UIControlStateNormal];
    }
    else if (self.uploadingType == BXUploadingPhoteTypeSign)
    {
        [button setImage:[UIImage imageWithContentsOfFile:fullPath] forState:UIControlStateNormal];
    }
    else
    {
        [button setImage:[UIImage imageWithContentsOfFile:fullPath] forState:UIControlStateNormal];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName{
    
    //高保真压缩图片方法
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    /*错误写法, 要注意我们写到本地的是urlPath --->stringByAppendingPathComponent**/
    //    NSString *fullPath = [[NSHomeDirectory()stringByAppendingString:@"Documents"]stringByAppendingString:imageName];
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
    
    /**
     *  写入文件 必须是以data类型的才行(所以我们要把image转化为data类型)
     *  将图片写入文件
     **/
    
    [imageData writeToFile:fullPath atomically:NO];
}


//时间
-(NSString*)creatTimeData
{
    NSDateFormatter *dateFormateter = [[NSDateFormatter alloc] init];
    [dateFormateter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    NSString *dataString = [dateFormateter stringFromDate:[NSDate date]];
    return dataString;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值