这段时间需要整理一些东西, 先备注在这里, 将花时间把这个点整理一下。
使用AFNetworking上传图片,(可一次上传多张图片,包含不同类型png, jpeg)
使用AFNetworking上传视频
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [manager POST:mutPath
parameters:param
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (mediaDatas.count > 0) {
NSObject *firstObj = [mediaDatas objectAtIndexSafe:0];
if ([firstObj isKindOfClass:[UIImage class]]) { // 图片
for(NSInteger i=0; i<mediaDatas.count; i++) {
UIImage *eachImg = [mediaDatas objectAtIndexSafe:i];
//NSData *eachImgData = UIImagePNGRepresentation(eachImg);
NSData *eachImgData = UIImageJPEGRepresentation(eachImg, 0.5);
[formData appendPartWithFileData:eachImgData name:[NSString stringWithFormat:@"img%d", i+1] fileName:[NSString stringWithFormat:@"img%d.jpg", i+1] mimeType:@"image/jpeg"];
}
}else { // 视频
ALAsset *asset = [mediaDatas objectAtIndexSafe:0];
NBLog(@"asset=%@, representation=%@, url=%@", asset, [asset defaultRepresentation], [asset defaultRepresentation].url);
if (asset != nil) {
NSString *videoPath = [NSDocumentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.mov", 0]]; // 这里直接强制写一个即可,之前计划是用i++来区分不明视频
NSURL *url = [NSURL fileURLWithPath:videoPath];
NSError *theErro = nil;
BOOL exportResult = [asset exportDataToURL:url error:&theErro];
NBLog(@"exportResult=%@", exportResult?@"YES":@"NO");
NSData *videoData = [NSData dataWithContentsOfURL:url];
[formData appendPartWithFileData:videoData name:@"video1" fileName:@"video1.mov" mimeType:@"video/quicktime"];
NBLog(@"method 2");
}
}
}
} success:^(AFHTTPRequestOperation *operation, idresponseObject) {
NSDictionary *returnedDic = [XXBaseViewController parseResponseObj:responseObject];
NBLog(@"post Big success returnedDic=%@", returnedDic);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NBLog(@"post big file fail error=%@", error);
if (errorBlock) {
errorBlock(@{@"errorcode":@(error.code),@"errordomain":error.domain});
}
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long longtotalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"bytesWritten=%d, totalBytesWritten=%lld, totalBytesExpectedToWrite=%lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
if (xxProgressView != nil) {
[xxProgressView setProgressViewTo:totalBytesWritten*1.0/totalBytesExpectedToWrite];
}
}];
[formData appendPartWithFileData:eachImgData name:[NSString stringWithFormat:@"img%d", i+1] fileName:[NSString stringWithFormat:@"img%d.jpg", i+1] mimeType:@"image/jpeg"];
CTAssetsPickerController来选择手机相册中的视频文件的。
然后通过生成一个视频文件名及地址, 并通过一个写方法, 写到该路径下, 写文件如下。
- (BOOL) exportDataToURL: (NSURL*) fileURL error: (NSError**) error
{
[[NSFileManager defaultManager] createFileAtPath:[fileURL path] contents:nilattributes:nil];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingToURL:fileURL error:error];
if (!handle) {
return NO;
}
ALAssetRepresentation *rep = [self defaultRepresentation];
uint8_t *buffer = calloc(BufferSize, sizeof(*buffer));
NSUInteger offset = 0, bytesRead = 0;
do {
@try {
bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:error];
[handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]];
offset += bytesRead;
} @catch (NSException *exception) {
free(buffer);
return NO;
}
} while (bytesRead > 0);
free(buffer);
return YES;
}
(估计这里可能有更好的办法来实现这个功能, 因为上面这个写入文件后,再转成NSData感觉有些繁琐,因为我曾尝试过其它方法,如通过ALAsset来获取到这个视频文件的url地址, 然后再通过NSData直接取这个地址,结果发现上传给后台后, 后台并不能识别到这个视频文件,后台能知道确实收到了视频数据,但可能某些原因,使得前端再次去获取该视频文件时,发现播放不了。具体原因可以再次去进行研究。
尝试过程如下:
A:
上传后, 后台仅获取到文本
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *videoData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
// [formData appendPartWithFormData:videoData name:@"video1"];
[formData appendPartWithFileData:videoData name:@"video1" fileName:@"video1.mov" mimeType:@"video/quicktime"];
尝试B:通过NSURl来取到这个地址, 然后用NSData来取视频, 然后再把这个视频进行上传。
)
有兴趣的朋友可以继续研究下去。
注:上传视频时和上面的上传图片一样,需要指定这个.mov, 及video/quicktime 类型指定。 前面的方法中要进行存储到本地时, 文件名指定为%d.mov。