AFNetworking2.X +Dbcamara 上传图片、文件

- (void) camera:(id)cameraViewController didFinishWithImage:(UIImage *)image withMetadata:(NSDictionary *)metadata
{
    NSLog(@"----------picDic%@",metadata);
    NSLog(@"width:%f height:%f",image.size.width, image.size.height);
    UIImage *_photo =  [AppDelegate imageWithImageSimple:image scaledToSize:CGSizeMake(image.size.width/5, image.size.height/5)];
    NSLog(@"width:%f height:%f",image.size.width, image.size.height);//这里是压缩后的大小 后面会有该类方法的写法
    NSString *fileName = [NSString stringWithFormat:@"%@.png",[NSString generateGuid]];
    [AppDelegate saveImage:_photo WithName:fileName];<span style="font-family: Arial, Helvetica, sans-serif;">//这里是保存图片 会有该类方法的写法</span>

    NSArray *patharray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path =  [patharray objectAtIndex:0];
    NSString *filepath2=[path stringByAppendingPathComponent:fileName];//添加我们需要的文件全称
    NSURL *filePath = [NSURL URLWithString:filepath2];
   [cameraViewController restoreFullScreenMode];
    NSLog(@"拍照完成");
    NSLog(@"%@",filePath);//这里是获取拍照后得到图片的地址  实际上是后者上传的是nsdata,所以这个路径可以去掉,如果需要传文件,可以使用该地址

    
    
    NSString *userid=[NSString stringWithFormat:@"%d",[AppDelegate userId]];
    NSDictionary *dic =@{@"userid":userid};
 
    
    AFHTTPRequestOperationManager *af = [AFHTTPRequestOperationManager manager];
    [af.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
   
    AFHTTPRequestOperation *afOperation = [af POST:@"url" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSData *data = UIImageJPEGRepresentation(_photo, 1);
            [formData appendPartWithFileData:data name:@"filename" fileName:@"img" mimeType:@"image/jpeg"];
       //<span style="font-family: Arial, Helvetica, sans-serif;">name  后面给的</span><span style="font-family: Arial, Helvetica, sans-serif;">filename是后台上传图片   fileName 是逻辑地址,可以随便命名?这点不是很肯定</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span>

        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
        NSLog(@"responseString : %@",operation.responseString);
        [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
       
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"responseString : %@",operation.responseString);
         [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
       
      
    }];
    [afOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        float progress =(float)totalBytesWritten/ (float)totalBytesExpectedToWrite;
        //            NSLog(@"%f",progress);
        NSLog(@"%f",progress);//这里是回调上传进度条
      
    }];
    
}


+ (UIImage*)imageWithImageSimple:(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
    [image drawInRect: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.
    return newImage;
}

+(void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
}


如果没有使用Dbcamara 上传写法为 

 NSString *userid=[NSString stringWithFormat:@"%d",[AppDelegate userId]];
    NSDictionary *dic =@{@"userid":userid};
 
    
    AFHTTPRequestOperationManager *af = [AFHTTPRequestOperationManager manager];
    [af.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
   
    AFHTTPRequestOperation *afOperation = [af POST:@"url" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSData *data = UIImageJPEGRepresentation(_photo, 1);
            [formData appendPartWithFileData:data name:@"filename" fileName:@"img" mimeType:@"image/jpeg"];
       //name  后面给的filename是后台上传图片   fileName 是逻辑地址,可以随便命名?这点不是很肯定


        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.navigationController popViewControllerAnimated:YES];
        NSLog(@"responseString : %@",operation.responseString);
        [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
       
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"responseString : %@",operation.responseString);
         [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
       
      
    }];
    [afOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        float progress =(float)totalBytesWritten/ (float)totalBytesExpectedToWrite;
        //            NSLog(@"%f",progress);
        NSLog(@"%f",progress);//这里是回调上传进度条


猜你喜欢

转载自blog.csdn.net/w250130255/article/details/43307871