尝试将视频文件发布到服务器,以字节为单位发布。我在下面发布我的代码用于发布。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ( [mediaType isEqualToString:@"public.movie" ])
{
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo)
{
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoUrl];
[self post:webData];
if([profile isEqualToString:@"friend"])
{
[self callServerFriend];
}
if([profile isEqualToString:@"public"])
{
[self callServerPublic];
}
}
}
- (void)post:(NSData *)fileData
{
NSLog(@"POSTING");
index1 = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoName"];
valIndex1 = index1;
NSString *str = [NSString stringWithFormat:@"video_%d.mp4",valIndex];
// Generate the postdata:
NSData *postData = [self generatePostDataForData: fileData];
profileImg = profilePic;
NSArray *arrImg = [profileImg componentsSeparatedByString:@"/"];
NSString *strImg1 = [arrImg objectAtIndex:[arrImg count]-2];
NSString *strImg2 = @"/";
NSString *strImg3 = [arrImg lastObject];
//NSLog(@"img2-----------------%@",strImg2);
NSString *pImg = [NSString stringWithFormat:@"%@%@%@",strImg1, strImg2, strImg3];
NSLog(@"img-----------------%@",pImg);
// Setup the request:
NSString *urlString = [[NSString stringWithFormat:@"http://www.myvnt.co/overheadPost.php?userId=%@&type=Video&text=%@&image=%@&location=%@&Upload=Upload", uid, statusPost, pImg, locc]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *wigiRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[wigiRequest setHTTPMethod:@"POST"];
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
stringBoundary];
// set header
[wigiRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
//add body
NSMutableData *postBody = [NSMutableData data];
NSLog(@"body made");
//IMAGE Posting
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//"Content-Disposition: form-data; name=\"fileUpload\";filename=\"" + imagePath + "\"" + lineEn
//[postBody appendData:[@"Content-Disposition: form-data; name=\"fileUpload\"; filename=\"img.mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileUpload\"; filename=\"%@\"\r\n", str]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"uploadFile\"; filename=\"img.mp4\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
//[postData setData:postHeaderData];
// add it to body
[postBody appendData:postData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[wigiRequest setHTTPBody:postBody];
NSLog(@"body set");
// pointers to some necessary objects
NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init];
NSError* error = [[NSError alloc] init] ;
// synchronous filling of data from HTTP POST response
NSData *respData = [NSURLConnection sendSynchronousRequest:wigiRequest returningResponse:&response error:&error];
NSLog(@"just sent request");
NSString *responseHTML = [[NSString alloc] initWithBytes:[respData bytes]
length:[respData length]
encoding:NSUTF8StringEncoding];
NSLog(@"response dictionary : %@",responseHTML);
index1++;
[[NSUserDefaults standardUserDefaults] setInteger:index forKey:@"videoName"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSData *)generatePostDataForData:(NSData *)uploadData
{
// Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"uploadFile\"; filename=\"img.mp4\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
[postData setData:postHeaderData];
// Add the image:
[postData appendData: uploadData];
// Add the closing boundry:
[postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
另一件事是它每次发布同名视频,所以每个新帖子都会替换前一个。但主要问题是为什么它以字节为单位发布我没有得到。 请指导以上。提前谢谢。
答案 0 :(得分:0)
试试这个,我用当前的Date-Time ::
存储了它-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[self dismissViewControllerAnimated:NO completion:nil];
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{
videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"found a video");
// Code To give Name to video and store to DocumentDirectory //
videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"dd-MM-yyyy||HH:mm:SS"];
NSDate *now = [[[NSDate alloc] init] autorelease];
theDate = [dateFormat stringFromDate:now];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
NSString *videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,theDate]] autorelease];
BOOL success = [videoData writeToFile:videopath atomically:NO];
NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
NSLog(@"video path --> %@",videopath);
}
}
视频上传::
videoData
来自videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
- (void)uploadVideo
{
NSData *imageData = videoData;
NSString *urlString=[NSString stringWithFormat:@"%s", UploadVideoService];
NSLog(@"url=== %@", urlString);
request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/* body of the post */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Video Name with Date-Time
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd-hh:mm:ssa"];
NSString *currDate = [dateFormat stringFromDate:[NSDate date]];
NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"video-%@.mov\"\r\n", currDate];
NSLog(@"String name:: %@",str);
[dateFormat release];
[body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"result from webservice:::--> %@", returnString);
[returnString release];
}
希望,它会帮助你。
感谢。