2

我已经构建了一个 iPhone 应用程序,我想将存储在用户 iPhone 中的视频、图像和音频上传到服务器。我已经成功地为存储在照片库中的视频和图像做到了这一点。代码如下:

      imageData = UIImageJPEGRepresentation(image.image, 90);

      NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

       if (image_selected)  {
                stringForImagePathToUpload = [[NSString alloc]initWithFormat:@"..."];
                [request setURL:[NSURL URLWithString:stringForImagePathToUpload]];
            }else if (video_selected){
                stringForVideoPathToUpload = [[NSString alloc]initWithFormat:@"..."];
                [request setURL:[NSURL URLWithString:stringForVideoPathToUpload]];
            }

        [request setHTTPMethod:@"POST"];

        NSString *boundary = [NSString stringWithString:@"*****"]; 

        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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        if (image_selected) {
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\";filename=\"%@"".jpg""\r\n\r\n", imageTitle.text] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[NSData dataWithData:imageData]];
        }else if (video_selected){

            NSFileHandle *file = [NSFileHandle fileHandleForReadingFromURL:videoURL error:nil];
            NSData *data = [file readDataToEndOfFile];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\";filename=\"%@"".m4v""\r\n\r\n", imageTitle.text] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[NSData dataWithData:data]];
        }

    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

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

    /* setting the body of the post to the reqeust */

    [request setHTTPBody:body];

    /* setting up the connection to the web*/
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];



    NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]autorelease];

    NSLog(@"Content uploaded %@", returnString);

现在,我发现音频文件存储在 iPhone 的 ipod 应用程序中。我想知道我是否可以像访问照片库中的图像和视频一样访问此音频的路径。如果可以访问音频文件,请帮助我了解如何访问它们。

4

0 回答 0