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) {
        //判斷是否可以打開相機,模擬器無法使用此功能
        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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市风范,隨后出現(xiàn)的幾起案子咨跌,更是在濱河造成了極大的恐慌,老刑警劉巖硼婿,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锌半,死亡現(xiàn)場離奇詭異,居然都是意外死亡加酵,警方通過查閱死者的電腦和手機拳喻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來猪腕,“玉大人冗澈,你說我怎么就攤上這事÷希” “怎么了亚亲?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長腐缤。 經(jīng)常有香客問我捌归,道長,這世上最難降的妖魔是什么岭粤? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任惜索,我火速辦了婚禮,結(jié)果婚禮上剃浇,老公的妹妹穿的比我還像新娘巾兆。我一直安慰自己,他們只是感情好虎囚,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布角塑。 她就那樣靜靜地躺著,像睡著了一般淘讥。 火紅的嫁衣襯著肌膚如雪圃伶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機與錄音窒朋,去河邊找鬼搀罢。 笑死,一個胖子當著我的面吹牛炼邀,可吹牛的內(nèi)容都是我干的魄揉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼拭宁,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瓣俯?” 一聲冷哼從身側(cè)響起杰标,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎彩匕,沒想到半個月后腔剂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡驼仪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年掸犬,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绪爸。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡湾碎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出奠货,到底是詐尸還是另有隱情介褥,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布递惋,位于F島的核電站柔滔,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏萍虽。R本人自食惡果不足惜睛廊,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望杉编。 院中可真熱鬧超全,春花似錦、人聲如沸王财。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绒净。三九已至见咒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挂疆,已是汗流浹背改览。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工下翎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人宝当。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓视事,卻偏偏與公主長得像,于是被迫代替她去往敵國和親庆揩。 傳聞我的和親對象是個殘疾皇子俐东,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽?zāi)J的外補...
    _Yfling閱讀 13,754評論 1 92
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,146評論 25 707
  • 你的嘴角有腐爛的殘留 你根本聽不見海風的聲音 面具遮住了你的眼睛 就以為看不到周身蔓延的疤痕 你怎么還不閉嘴 沒有...
    甘野閱讀 235評論 0 0
  • 一年一度的慶六一藝術(shù)節(jié)展演就要到來了订晌,為了使藝術(shù)節(jié)的氣氛更加熱鬧虏辫,學校加進了一項拉歌活動。于是為了幫助音樂老師锈拨,我...
    蒹葭essay閱讀 313評論 0 0
  • 這個文章寫的很詳細砌庄,有什么地方忘記的時候,可以過來參考一眼奕枢。http://www.reibang.com/p/a6...
    LGirl閱讀 268評論 0 0