用IOS应用上传图片到服务器文件过大 [英] Uploading image with IOS app to server file size is too large

查看:18
本文介绍了用IOS应用上传图片到服务器文件过大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 IOS 应用程序将照片上传到服务器.重要的是,照片上传时没有质量损失,并以 jpeg 格式上传.我目前的问题是上传的照片没有质量损失,但文件大小大于预期.例如:我通过应用上传了一个文件,文件大小为 4.7 MB.当我将同一张照片通过电子邮件发送给自己并为电子邮件选择实际照片"选项时,照片的大小仅为 1.7 MB.并排比较显示质量没有差异.

I am uploading photos to a server with an IOS app. It is important that the photos are uploaded with no loss in quality and are uploaded as jpeg's. My current problem is that the photos upload with no loss of quality but have a larger than expected file size. For example: I uploaded a file through the app and the file size was 4.7 MB. When I Emailed the same photo to myself and selected the "Actual Photo" option for the email, The size of the photo was only 1.7 MB. A side by side comparison revealed no difference in quality.

这是我上传文件的方式.

Here is how I am uploading the files.

ALAssetsLibrary *library = [ALAssetsLibrary new];
[library getImageAtURL:orderImage.imageUrl with completionBlock:^(UIImage *image)

NSData *fileData = UIImageJPEGRepresentation(image, 1.0)

NSURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
    [formData appendPartWithFileData:fileData name:@"uploadedfile" fileName:fileName mimeType:mimeType];
    [formData appendPartWithFormData:[extraInfo dataUsingEncoding:NSISOLatin2StringEncoding] name:@"extraInfo"];
}];

推荐答案

问题在于UIImageJPEGRepresentation.它不会检索原始 JPEG,而是创建一个新的 JPEG.当您使用 1compressionQuality(大概是为了避免进一步的图像质量损失)时,它会创建这种没有压缩的新表示(通常会导致文件大于原始文件)).

The problem is UIImageJPEGRepresentation. It does not retrieve the original JPEG, but rather creates a new JPEG. And when you use a compressionQuality of 1 (presumably to avoid further image quality loss), it creates this new representation with no compression (generally resulting in a file larger than the original).

我建议使用 getBytes 来检索原始资产,而不是通过 UIImage 来回传输它并通过 UIImageJPEGRepresentation 获取数据:

I would advise using getBytes to retrieve the original asset, rather than round-tripping it through a UIImage and getting the data via UIImageJPEGRepresentation:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetsLibraryURL resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *representation = [asset defaultRepresentation];

    // I generally would write directly to a `NSOutputStream`, but if you want it in a
    // NSData, it would be something like:

    NSMutableData *data = [NSMutableData data];

    // now loop, reading data into buffer and writing that to our data stream

    NSError *error;
    long long bufferOffset = 0ll;
    NSInteger bufferSize = 10000;
    long long bytesRemaining = [representation size];
    uint8_t buffer[bufferSize];
    while (bytesRemaining > 0) {
        NSUInteger bytesRead = [representation getBytes:buffer fromOffset:bufferOffset length:bufferSize error:&error];
        if (bytesRead == 0) {
            NSLog(@"error reading asset representation: %@", error);
            return;
        }
        bytesRemaining -= bytesRead;
        bufferOffset   += bytesRead;
        [data appendBytes:buffer length:bytesRead];
    }

    // ok, successfully read original asset; 
    // do whatever you want with it here

} failureBlock:^(NSError *error) {
    NSLog(@"error=%@", error);
}];

--

如果你使用的是iOS 8引入的Photos框架,可以使用PHImageManager来获取图片数据:

If you're using the Photos framework introduced in iOS 8, can use PHImageManager to get the image data:

PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetsLibraryURL] options:nil];
PHAsset *asset = [result firstObject];
if (asset) {
    PHImageManager *manager = [PHImageManager defaultManager];
    [manager requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
        // use `imageData` here
    }];
}

这篇关于用IOS应用上传图片到服务器文件过大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆