彈出操作表從相冊(cè)或拍照中獲取圖片信息,并壓縮保存到本地,最后上傳到公司服務(wù)器。

注意事項(xiàng):公司后臺(tái)發(fā)送請(qǐng)求后,得到的錯(cuò)誤結(jié)果是JSON text did not start with array or object and option to allow fragments not set;

這是因?yàn)?AFNetworking默認(rèn)把響應(yīng)結(jié)果當(dāng)成json來處理,(默認(rèn)manager.responseSerializer = [AFJSONResponseSerializer serializer]) ,很顯然,我們請(qǐng)求的百度首頁 返回的并不是一個(gè)json文本,而是一個(gè)html網(wǎng)頁,但是AFNetworking并不知道,它堅(jiān)信請(qǐng)求的結(jié)果就是一個(gè)json文本!然后固執(zhí)地以json的形式去解析,顯然沒辦法把一個(gè)網(wǎng)頁解析成一個(gè)字典或者數(shù)組,所以產(chǎn)生了上述錯(cuò)誤.

然而,我們期望它能夠正確地處理這個(gè)情形,而不是提示一個(gè)錯(cuò)誤.

這時(shí)候 你必須告訴AFNetworking:別把這個(gè)網(wǎng)頁當(dāng)json來處理!

只需要在發(fā)送請(qǐng)求前加入:manager.responseSerializer = [AFHTTPResponseSerializer serializer];


主要代碼如下:

@interfaceMeTableViewController()

UIImagePickerControllerDelegate,

UINavigationControllerDelegate,UITabBarControllerDelegate>

/**

*我的頭像

*/

@property(weak,nonatomic)IBOutletUIImageView*myImageView;

@property(nonatomic,strong)UIAlertController* alertController;

@property(nonatomic,strong)UIAlertAction* cancelAction;

@property(nonatomic,strong)UIAlertAction* cameraAction;

@property(nonatomic,strong)UIAlertAction* albumAction;

@property(nonatomic,strong)KUserInfo* userInfo;

@property(nonatomic,strong)NSString* token;

@property(nonatomic,assign)NSIntegerselectedIndex;

/**

*我的愛心幣

*/

@property(weak,nonatomic)IBOutletUILabel*myLoveMoneyLabel;

/**

*我的昵稱

*/

@property(weak,nonatomic)IBOutletUILabel*myUserNameLabel;

/**

*我的捐款總額

*/

@property(weak,nonatomic)IBOutletUIButton*launchBtn;

@property(weak,nonatomic)IBOutletUIButton*donateBtn;

@property(weak,nonatomic)IBOutletUILabel*myDonateTotalLabel;

@property(nonatomic,strong)NSIndexPath* indexPath;

@property(nonatomic,strong)NSString* filePath;

@end

@implementationMeTableViewController

#pragma mark-----------懶加載

- (UIAlertController*)alertController

{

if(!_alertController) {

_alertController= [UIAlertControlleralertControllerWithTitle:@"請(qǐng)選擇"message:@"獲取照片的來源"preferredStyle:UIAlertControllerStyleActionSheet];

[_alertControlleraddAction:self.cancelAction];

[_alertControlleraddAction:self.cameraAction];

[_alertControlleraddAction:self.albumAction];

}

return_alertController;

}

- (UIAlertAction*)cancelAction

{

if(!_cancelAction) {

_cancelAction= [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {

[selfdismissViewControllerAnimated:YEScompletion:nil];

}];

}

return_cancelAction;

}

- (UIAlertAction*)cameraAction

{

if(!_cameraAction) {

_cameraAction= [UIAlertActionactionWithTitle:@"照相機(jī)"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*_Nonnullaction) {

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

UIImagePickerController*pc = [[UIImagePickerControlleralloc]init];

pc.allowsEditing=YES;

pc.sourceType=UIImagePickerControllerCameraCaptureModeVideo;

pc.delegate=self;

[selfpresentViewController:pcanimated:YEScompletion:nil];

}

}];

}

return_cameraAction;

}

- (UIAlertAction*)albumAction

{

if(!_albumAction) {

_albumAction= [UIAlertActionactionWithTitle:@"相冊(cè)"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

UIImagePickerController*pc = [[UIImagePickerControlleralloc]init];

pc.allowsEditing=YES;

pc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

pc.delegate=self;

[selfpresentViewController:pcanimated:YEScompletion:nil];

}];

}

return_albumAction;

}

- (KUserInfo*)userInfo

{

if(!_userInfo) {

_userInfo= [KUserInfosharedUserInfo];

}

return_userInfo;

}


#pragma mark----------------------------------------------------viewDidLoad

- (void)viewDidLoad {

[superviewDidLoad];

if(self.userInfo.avator) {

[self.myImageViewsetImageWithURL:[NSURLURLWithString:self.userInfo.avator]];

}else{

//默認(rèn)頭像

}


}


//添加圖片圓角并加tap手勢(shì)

- (void)setUpImageView{

[self.myImageViewsetRoundLayer];

[self.myImageViewaddGestureRecognizer:[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(headImageTap)] ];

}

/*圖片tap方法的處理,跳轉(zhuǎn)到alertController*/

- (void) headImageTap

{

[selfpresentViewController:self.alertControlleranimated:YEScompletion:nil];

}

#pragma mark----------UIImagePickerControllerDelegate

/**選擇圖片的處理*/

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

UIImage*image = info[UIImagePickerControllerEditedImage];

self.myImageView.image= image;

//保存到沙盒

[selfsaveImage:imageWithName:@"avator"];

[self dismissViewControllerAnimated:YEScompletion:nil];

}

//保存圖片

- (void)saveImage:(UIImage*)tempImage WithName:(NSString*)imageName

{

UIImage*imageNew = tempImage;

//設(shè)置image的尺寸(可以自己制定)

CGSizeimagesize = imageNew.size;

imagesize.height=626;

imagesize.width=413;

//對(duì)圖片大小進(jìn)行壓縮--

imageNew = [selfimageWithImage:imageNewscaledToSize:imagesize];

NSData*imageData =UIImageJPEGRepresentation(imageNew,0.00001);

//return image to png

// NSData* imageData = UIImagePNGRepresentation(tempImage);

NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];

NSString* totalPath = [documentPathstringByAppendingPathComponent:imageName];

self.filePath= totalPath;

//保存到document

[imageDatawriteToFile:totalPathatomically:NO];

//保存到NSUserDefaults

NSUserDefaults*userDefaults = [NSUserDefaultsstandardUserDefaults];

[userDefaultssetObject:totalPathforKey:@"avatar"];

//上傳服務(wù)器

NSLog(@"圖片文件地址:%@",self.filePath);

dispatch_async(dispatch_get_global_queue(0,0), ^{

NSLog(@"3%@",[NSThreadcurrentThread]);

[selfsendChangeUserAvatorRequest];

});

}

//對(duì)圖片尺寸進(jìn)行壓縮--(壓縮前3.2M,壓縮后為16K)

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize

{

// Create a graphics image context

UIGraphicsBeginImageContext(newSize);

// Tell the old image to draw in this new context, with the desired

// new size

[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context

UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

// End the context

UIGraphicsEndImageContext();

// Return the new image.

returnnewImage;

}

#pragma mark--------------發(fā)送更換用戶頭像的請(qǐng)求(自己對(duì)AFNetworking重新進(jìn)行封裝)kCHANGEAVATORSTR是.pch中聲明的各種參數(shù)

- (void)sendChangeUserAvatorRequest

{

[AFHTTPSessionManagerGET:kCHANGEAVATORSTR parameters:nilcompletionHandle:^(idmodel,NSError*error){

if(!error) {

[selfshowAlert:@"頭像修改成功"];

}else{

NSLog(@"error:%@",error.userInfo);

[selfshowAlert:@"頭像修改失敗"];

}

}];

}

@end


主要用于個(gè)人記錄,供參考!!!!!!!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,197評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,415評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,104評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,884評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,647評(píng)論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,130評(píng)論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,208評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,366評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,887評(píng)論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,737評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,939評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,478評(píng)論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,174評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,586評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,827評(píng)論 1 283
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,608評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,914評(píng)論 2 372

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