我想从UIImagePicker同时上传图片和视频

时间:2016-07-30 11:58:06

标签: objective-c iphone ios7 xcode7

我想同时上传图片和视频,我尝试过的轰鸣声编码,但没有上传到服务器,我想要一个视频缩略图,所以我将从同一个视频和我要上传的图像和视频中获取缩略图到服务器

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

               NSLog(@"Image And Video will be uploaded");

               img = [info valueForKey:UIImagePickerControllerOriginalImage];

    NSURL *imurl=[info valueForKeyPath:UIImagePickerControllerMediaURL];
    UIImage *croppedImage = [appdel imageWithImage:img scaledToWidth:400];

             NSString *finalUniqueImageNAme1;
             NSString *finalUniqueImageNAme2;


                thumbimages = [self thumbnailImageFromURL:imurl];

                NSData *videoData = [NSData dataWithContentsOfURL:imurl];




                if(videoData.length)
                {

                    NSDate *currentDate = [[NSDate alloc] init];
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

                    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
                    NSString *localDateString = [dateFormatter stringFromDate:currentDate];
                    NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
                    NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1];

                finalUniqueImageNAme1 = [cleanedString2 stringByAppendingString:@".mp4"];

                }


                if(thumbimages != nil)
                {

                    NSDate *currentDate = [[NSDate alloc] init];
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

                    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
                    NSString *localDateString = [dateFormatter stringFromDate:currentDate];
                    NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
                    NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1];



                     finalUniqueImageNAme2 = [cleanedString2 stringByAppendingString:@".jpg"];


                }
                 NSData *imageData = UIImageJPEGRepresentation(thumbimages, 90);
                NSString *urlString = @"http://vygears.com/tcdc-portfolio/Abdul/chat/upload/Vupload_file.php";
                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [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"];

                NSMutableData *body = [NSMutableData data];
                [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];



                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file1\"; filename=\"%@\"\r\n",finalUniqueImageNAme1] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file2\"; filename=\"%@\"\r\n",finalUniqueImageNAme2] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];




                    [body appendData:[NSData dataWithData:videoData]];

                    [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];

              // NSLog(@"Successfully uploaded");

                NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
                if(conn)
                {
                    NSLog(@"Connection Successful");
                  //  [self dismissModalViewControllerAnimated:true];
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
                else
                {
                    NSLog(@"Connection could not be made");
                }
            }
        }

        - (UIImage *)thumbnailImageFromURL:(NSURL *)videoURL {

            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL: videoURL options:nil];
            AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
            NSError *err = NULL;
            CMTime requestedTime = CMTimeMake(12,13);     // To create thumbnail image
            CGImageRef imgRef = [generator copyCGImageAtTime:requestedTime actualTime:NULL error:&err];
            NSLog(@"err = %@, imageRef = %@", err, imgRef);
            thumbnailImage = [[UIImage alloc] initWithCGImage:imgRef];
            CGImageRelease(imgRef);    // MUST release explicitly to avoid memory leak
            return thumbnailImage; 
        }

1 个答案:

答案 0 :(得分:2)

最后,我花了一些时间来获得代码

 img = [info valueForKey:UIImagePickerControllerOriginalImage];

NSURL *imurl=[info valueForKeyPath:UIImagePickerControllerMediaURL];
UIImage *croppedImage = [appdel imageWithImage:img scaledToWidth:400];
NSLog(@"Image And Video will be uploaded");

        NSString *finalUniqueImageNAme1;
        NSString *finalUniqueImageNAme2;
        NSData *videoData = [NSData dataWithContentsOfURL:imurl];
        thumbimages = [self thumbnailImageFromURL:imurl];


        if(videoData.length)
        {

            NSDate *currentDate = [[NSDate alloc] init];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

            [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
            NSString *localDateString = [dateFormatter stringFromDate:currentDate];
            NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
            NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1];

            finalUniqueImageNAme1 = [cleanedString2 stringByAppendingString:@".mp4"];

        }


        if(thumbimages != nil)
        {

            NSDate *currentDate = [[NSDate alloc] init];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

            [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
            NSString *localDateString = [dateFormatter stringFromDate:currentDate];
            NSString* cleanedString = [[localDateString stringByReplacingOccurrencesOfString:@"." withString:@""]stringByReplacingOccurrencesOfString:@":" withString:@""];
            NSString *cleanedString2 = [cleanedString stringByAppendingFormat:@"%d",1];



            finalUniqueImageNAme2 = [cleanedString2 stringByAppendingString:@".jpg"];


        }

        NSData *imageData = UIImageJPEGRepresentation(thumbimages, 90);

        NSString *urlString = @"http://vygears.com/tcdc-portfolio/Abdul/chat/upload/Vupload_file.php";
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [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"];
        NSMutableData *body = [NSMutableData data];

        if (imageData)
        {


            //======= Image Data=======//

            [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file2\"; filename=\"%@\"\r\n",finalUniqueImageNAme2] 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]];

            //======= Video Data=======//


            [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file1\"; filename=\"%@\"\r\n",finalUniqueImageNAme1] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

            [body appendData:[NSData dataWithData:videoData]];
            [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        }

        [request setHTTPBody:body];
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if(conn)
        {
            NSLog(@"Connection Successful");
            //  [self dismissModalViewControllerAnimated:true];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            NSLog(@"Connection could not be made");
        }
    }