注意事項(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è)人記錄,供參考!!!!!!!