美文网首页
使用AFNetWorking上传照片

使用AFNetWorking上传照片

作者: Loki_ | 来源:发表于2016-06-02 11:13 被阅读143次

    1、照片选择完成

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

    {

    [pickerdismissViewControllerAnimated:YEScompletion:nil];

    photo= [infoobjectForKey:UIImagePickerControllerEditedImage];

    CGSizeimagesize =photo.size;

    imagesize.height=imagesize.height*1024/imagesize.width;

    imagesize.width=1024;

    //对图片大小进行压缩--

    photo= [ImageUtilimageWithImage:photoscaledToSize:imagesize];

    [self.tableViewreloadData];

    [selfimageUpToWebAction:photo];

    }

    2、上传照片

    #pragma mark ----上传图片

    -(void)imageUpToWebAction:(UIImage*)image

    {

    [selfshowMessage:@"头像上传中..."];

    NSString*urlString = [NSStringstringWithFormat:@"%@/photoupload",addressUrl];

    [managerPOST:urlStringparameters:nilconstructingBodyWithBlock:^(id formData) {

    NSData*imageData =UIImageJPEGRepresentation(image,0.00001);

    NSDateFormatter*formatter = [[NSDateFormatteralloc]init];

    formatter.dateFormat=@"yyyyMMddHHmmss";

    NSString*str = [formatterstringFromDate:[NSDatedate]];

    NSString*fileName = [NSStringstringWithFormat:@"%@.jpg", str];

    //上传图片,以文件流的格式

    [formDataappendPartWithFileData:imageDataname:@"importFile"fileName:fileNamemimeType:@"image/jpeg"];

    }success:^(AFHTTPRequestOperation*operation,idresponseObject) {

    [selfhideHUD];

    NSString*status=[responseObjectobjectForKey:@"status"];

    NSString*message=[responseObjectobjectForKey:@"message"];

    if([statusboolValue]){

    _urlString= [NSStringstringWithString:message];

    [selfsaveUserImage:message];

    }else{

    [selfshowError:message];

    }

    }failure:^(AFHTTPRequestOperation*operation,NSError*error) {

    [selfshowError:[error.userInfoobjectForKey:@"NSLocalizedDescription"]];

    }];

    }

    3、压缩照片

    //对图片尺寸进行压缩--

    +(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;

    }

    4、

    AFHTTPRequestOperationManager*manager;

    manager= [AFHTTPRequestOperationManagermanager];

    相关文章

      网友评论

          本文标题:使用AFNetWorking上传照片

          本文链接:https://www.haomeiwen.com/subject/ciqkdttx.html