iOS 中常見的設(shè)置、保存、上傳圖片

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) {
        //判斷是否可以打開相機(jī),模擬器無法使用此功能
        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) {
    }];
    //修改單個(gè)按鈕字體顏色
    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;
}


對圖片尺寸進(jìn)行壓縮

-(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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,125評論 1 92
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評論 25 709
  • 你的嘴角有腐爛的殘留 你根本聽不見海風(fēng)的聲音 面具遮住了你的眼睛 就以為看不到周身蔓延的疤痕 你怎么還不閉嘴 沒有...
    甘野閱讀 288評論 0 0
  • 一年一度的慶六一藝術(shù)節(jié)展演就要到來了,為了使藝術(shù)節(jié)的氣氛更加熱鬧,學(xué)校加進(jìn)了一項(xiàng)拉歌活動(dòng)。于是為了幫助音樂老師,我...
    蒹葭essay閱讀 379評論 0 0
  • 這個(gè)文章寫的很詳細(xì),有什么地方忘記的時(shí)候,可以過來參考一眼。http://www.itdecent.cn/p/a6...
    LGirl閱讀 296評論 0 0

友情鏈接更多精彩內(nèi)容