图像上传中的内存释放问题

时间:2016-09-28 12:44:54

标签: ios objective-c xcode

我正在尝试将图像数据上传到服务器。他们上传成功,但我看到每个图片上传的内存使用量达到峰值。此外,上传超过20张图片会导致应用关闭并导致内存警告。

以下是图片上传的代码。

NSDictionary * param =@{......};

manager.requestSerializer.timeoutInterval = 1200 ;// timeout for the bulk images
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];// set content type 
[manager POST:@"http://192.168.192.102:8080/xxxxxxxxxxxxxxxxxxxxxxxx" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    for (int j = 0; j< imageData.count; j++)
    {

        [formData appendPartWithFileData:imageData[j] name:[NSString stringWithFormat:@"%d",j+1] fileName:[NSString stringWithFormat:@"%d.jpg",j] mimeType:@"image/jpeg"];// add multipart data to request

    }

} progress:^(NSProgress * uploadProgress) {
    NSLog(@"%f",uploadProgress.fractionCompleted);// for how much upload it to server

} success:^(NSURLSessionDataTask * task, id  _Nullable responseObject) {
    NSLog(@"success");// success of the image upload

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * error) {
    NSLog(@"fail%@", error.localizedDescription);// error in the image upload
}];

1 个答案:

答案 0 :(得分:0)

我在xCode 7和iOS 9中使用它: -

-(void)uploadImages
    {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager POST:@"Your URL" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            int x=0;
            for (UIImage *imag in __chosenImages)
            {
                NSData *imageData = UIImagePNGRepresentation([self resizeImage:imag]);
                [formData appendPartWithFileData:imageData name:@"images[]" fileName:[NSString stringWithFormat:@"%d.png",x] mimeType:@"image/png"];
                x++;
            }

        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success: %@", responseObject );
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
    }
    -(UIImage *)resizeImage:(UIImage *)image
    {
        float actualHeight = image.size.height;
        float actualWidth = image.size.width;
        float maxHeight = 800.0;
        float maxWidth = 800.0;
        float imgRatio = actualWidth/actualHeight;
        float maxRatio = maxWidth/maxHeight;
        float compressionQuality = 1.0;//50 percent compression

        if (actualHeight > maxHeight || actualWidth > maxWidth)
        {
            if(imgRatio < maxRatio)
            {
                //adjust width according to maxHeight
                imgRatio = maxHeight / actualHeight;
                actualWidth = imgRatio * actualWidth;
                actualHeight = maxHeight;
            }
            else if(imgRatio > maxRatio)
            {
                //adjust height according to maxWidth
                imgRatio = maxWidth / actualWidth;
                actualHeight = imgRatio * actualHeight;
                actualWidth = maxWidth;
            }
            else
            {
                actualHeight = maxHeight;
                actualWidth = maxWidth;
            }
        }

        CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
        UIGraphicsBeginImageContext(rect.size);
        [image drawInRect:rect];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
        UIGraphicsEndImageContext();

        return [UIImage imageWithData:imageData];

    }