AFNetworking2.X +Dbcamara 上傳圖片、檔案
阿新 • • 發佈:2018-11-09
- (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
[email protected]{@"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 [email protected]{@"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);//這裡是回撥上傳進度條