iOS 中常见的设置、保存、上传图片

1.选择图片获取方式

-(void)doChange{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    __weak typeof(self) weakSelf = self;
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //判断是否可以打开相机,模拟器无法使用此功能
        if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
            UIImagePickerController *pic = [[UIImagePickerController alloc]init];
            pic.sourceType = UIImagePickerControllerSourceTypeCamera;
            pic.delegate = (id)weakSelf;
            pic.editing = YES;
            pic.allowsEditing = YES;
            [weakSelf presentViewController:pic animated:YES completion:nil];
        }
    }];
    
    UIAlertAction *album = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *pic = [[UIImagePickerController alloc]init];
        pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pic.delegate = (id)weakSelf;
        pic.editing = YES;
        pic.allowsEditing = YES;
        pic.editing = YES;
        [weakSelf presentViewController:pic animated:YES completion:nil];
    }];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    //修改单个按钮字体颜色
    camera.textColor = [UIColor hs_colorWithHex:0x5e5163];
    album.textColor = [UIColor hs_colorWithHex:0x5e5163];
    action.textColor = [UIColor hs_colorWithHex:0x9a9a9a];
    
    [alertController addAction:camera];
    [alertController addAction:album];
    [alertController addAction:action];
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - UIImagePickerController的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    //获得选中图片
    _image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //裁剪image的尺寸
    CGSize imagesize = _image.size;
    imagesize.height = 400;
    imagesize.width = 400;
    _image = [self imageWithImage:_image scaledToSize:imagesize];

    [self.sessionManager POST:URLStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}
- (AFHTTPSessionManager *)sessionManager {
    if (_sessionManager == nil) {
        _sessionManager = [AFHTTPSessionManager manager];
        _sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSSet *set = [NSSet setWithObjects:@"text/plain", @"text/html", nil];
        _sessionManager.responseSerializer.acceptableContentTypes = [_sessionManager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:set];
    }
    return _sessionManager;
}


对图片尺寸进行压缩

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

保存图片至沙盒

-(void)saveImage:(UIImage *)currenrImage WithName:(NSString *)imageName{
    NSData *imageData = UIImageJPEGRepresentation(currenrImage, 1);
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPath atomically:NO];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,694评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,274评论 25 709
  • 你的嘴角有腐烂的残留 你根本听不见海风的声音 面具遮住了你的眼睛 就以为看不到周身蔓延的疤痕 你怎么还不闭嘴 没有...
    甘野阅读 1,792评论 0 0
  • 一年一度的庆六一艺术节展演就要到来了,为了使艺术节的气氛更加热闹,学校加进了一项拉歌活动。于是为了帮助音乐老师,我...
    蒹葭essay阅读 2,662评论 0 0
  • 这个文章写的很详细,有什么地方忘记的时候,可以过来参考一眼。http://www.jianshu.com/p/a6...
    LGirl阅读 1,837评论 0 0

友情链接更多精彩内容