Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要帮助从ios设备上载多个大图像吗_Ios_Nsurlconnection_Afnetworking_Nsinputstream - Fatal编程技术网

需要帮助从ios设备上载多个大图像吗

需要帮助从ios设备上载多个大图像吗,ios,nsurlconnection,afnetworking,nsinputstream,Ios,Nsurlconnection,Afnetworking,Nsinputstream,我使用alasset库成功获取了iphone图像库中的所有图像url,并存储在一个数组中。现在我正在尝试上传到服务器,下面是我的代码: 我尝试了两种方法,但都是在迭代了大约10幅图像后崩溃的,没有任何崩溃日志。图像不上传到服务器,上传前会崩溃 1: NSData *imgData; UIImage *img; NSInputStream *stream; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithUR

我使用alasset库成功获取了iphone图像库中的所有图像url,并存储在一个数组中。现在我正在尝试上传到服务器,下面是我的代码:

我尝试了两种方法,但都是在迭代了大约10幅图像后崩溃的,没有任何崩溃日志。图像不上传到服务器,上传前会崩溃

1:

NSData *imgData; UIImage *img; NSInputStream *stream;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://my.url.com"]];

for(int i=0; i<_dataContainer.count; i++)
{
    img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:i] defaultRepresentation]fullResolutionImage]];
    imgData = UIImageJPEGRepresentation(img, 1.0);
    stream = [[NSInputStream alloc] initWithData:imgData];
    [request setHTTPBodyStream:stream];
    [request setHTTPMethod:@"POST"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
                           }];
}
NSData*imgData;UIImage*img;NSInputStream*流;
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@]http://my.url.com"]];

对于(int i=0;i您应该
[操作开始];
在设置完成和进度块回调之后。

您的崩溃可能是由于内存过载。首先,在第1节中,您需要在每次迭代时耗尽自动释放池,例如:

NSData *imgData; UIImage *img; NSInputStream *stream;

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://my.url.com"]];

    for(int i=0; i<_dataContainer.count; i++)
    {
        @autoreleasepool {
            img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:i] defaultRepresentation]fullResolutionImage]];
            imgData = UIImageJPEGRepresentation(img, 1.0);
            stream = [[NSInputStream alloc] initWithData:imgData];
            [request setHTTPBodyStream:stream];
            [request setHTTPMethod:@"POST"];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                       NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
                                   }];
        }
    }
@interface MyHTTPClient:AFHTTPClient
+(id)共享客户;
@结束
@MyHTTPClient的实现
+(id)共享客户端
{
静态MyHTTPClient*共享客户端;
静态调度一次;
一次发送(一次发送)^{
sharedClient=[[MyHTTPClient alloc]initWithBaseURL:nil];
});
返回共享客户;
}
@结束
@MyViewController的实现
-(无效)上载图像
{
NSURLRequest*myRequest;
__块UIImage*img;
__块NSData*imgData;
__块NSString*fName;
myRequest=[client multipartFormRequestWithMethod:@“POST”路径:@“/images/mypage.php”参数:nil constructingBodyWithBlock:
^(id formData)
{                     
img=[UIImage imageWithCGImage:[[[U dataContainer objectAtIndex:0]默认表示]完整解析图像]];
imgData=UIImageJPEG表示法(img,1.0);
fName=[self returndatetimewithmillizes];
[formData appendPartWithFileData:imgData名称:@“photo”文件名:[NSString stringWithFormat:@“%@.jpg”,fName]mimeType:@“image/jpeg”];
NSLog(@“FN=>%@| Size=>%@),fName,[NSByteCountFormatter stringFromByteCount:[imgData length]countStyle:NSByteCountFormatterCountStyleFile]);
}];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:myRequest];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象)
{
NSLog(@“成功数据->%@”,操作。响应记录);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“失败”);
}];
[操作设置LoadProgressBlock:^(NSUTEGER字节写入、long long TotalBytesWrite、long long totalBytesExpectedToWrite){
NSLog(@“Progrees->%f”,((float)((int)totalBytesWrite)/(float)((int)totalBytesExpectedToWrite));
}];
[[MyHTTPClient sharedClient]排队HttpRequestOperation:operation]
}
@结束

感谢您的回复…我尝试了autoreleasepool,但它仍然会崩溃,但持续时间增加,请您给我一个详细的代码片段如何使用[httpClient enqueueHTTPRequestOperation:operation]。非常感谢…它工作得很好。为了牢牢掌握内存管理,我试图做的是通过alasset库读取所有图像库url并存储在我的阵列中,现在我想上传所有图像,因为循环启动时应用程序崩溃,读取约10个图像,因为我有100多个大小为6MB的图像。为了解决此问题,我删除for循环并采用递归方法,一次读取一个图像上载它,并在其成功块中调用相同的方法以上载更多图像。但是现在我想上载一个像200mb+这样的大文件,正确的方法是什么。上载200mb+文件?这超出了我的理解范围。我猜应该使用initWithContentsOfFile:options:error:
使用NSDataReadingMappedaways选项,然后将其发送到上载请求中。我从未尝试过类似的操作,所以请玩玩并享受乐趣:)如果您真的陷入困境,我建议您打开一个有关StackOverflow的新问题,因为此问题已标记为已回答。
NSData *imgData; UIImage *img; NSInputStream *stream;

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://my.url.com"]];

    for(int i=0; i<_dataContainer.count; i++)
    {
        @autoreleasepool {
            img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:i] defaultRepresentation]fullResolutionImage]];
            imgData = UIImageJPEGRepresentation(img, 1.0);
            stream = [[NSInputStream alloc] initWithData:imgData];
            [request setHTTPBodyStream:stream];
            [request setHTTPMethod:@"POST"];
            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                       NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
                                   }];
        }
    }
[httpClient enqueueHTTPRequestOperation:operation]
@interface MyHTTPClient : AFHTTPClient

+ (id)sharedClient;

@end

@implementation MyHTTPClient

+ (id)sharedClient
{
    static MyHTTPClient *sharedClient;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClient = [[MyHTTPClient alloc] initWithBaseURL:nil];
    });
    return sharedClient;
}

@end

@implementation MyViewController

- (void)uploadImages
{
    NSURLRequest *myRequest;
    __block UIImage *img;
    __block NSData *imgData;
    __block NSString *fName;


    myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/images/mypage.php" parameters:nil constructingBodyWithBlock:
                 ^(id <AFMultipartFormData>formData)
                 {                     
                     img = [UIImage imageWithCGImage:[[[_dataContainer objectAtIndex:0] defaultRepresentation]fullResolutionImage]];
                     imgData = UIImageJPEGRepresentation(img, 1.0);
                     fName = [self returnDateTimeWithMilliSeconds];

                     [formData appendPartWithFileData:imgData name:@"photo" fileName:[NSString stringWithFormat:@"%@.jpg",fName] mimeType:@"image/jpeg"];

                     NSLog(@"FN=>%@ | Size=>%@",fName, [NSByteCountFormatter stringFromByteCount:[imgData length] countStyle:NSByteCountFormatterCountStyleFile]);
                 }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {

         NSLog(@"Success Data -> %@", operation.responseString);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Failed");
     }];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"Progrees -> %f", ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite)));
    }];

    [[MyHTTPClient sharedClient] enqueueHTTPRequestOperation:operation]
}

@end