Phonegap Build iOS camera.getPicture()质量参数不起作用 [英] Phonegap Build iOS camera.getPicture() quality parameter not working

查看:92
本文介绍了Phonegap Build iOS camera.getPicture()质量参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Phonegap应用程序,并使用Build服务对其进行了编译. 我的应用程序在以下代码块中调用相机:

I have written a Phonegap application and compiled it using the Build service. My application calls the camera in this block of code:

function capturePhoto(){

function capturePhoto() {

        // Take picture using device camera and retrieve image 
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
              quality: 20,
              destinationType: destinationType.FILE_URI });

    }

生成的图像通过ajax上传到服务器. 当我查看上载的图像时,有些到达时其质量会适当降低,但有些到达时的文件大小约为4 MB.我已经意识到在iPhone 4和5上运行我的应用程序的用户(我还没有6的测试人员)一直在上传大图像.

The resulting image is uploaded to a server via ajax. When I review the uploaded images, some arrive with their quality reduced appropriately but some arrive with a filesize of ~4 MB. I have realized that users who are running my app on iPhone 4 and 5 (I have no testers with 6 yet) are consistently uploading the large images.

我将画质降低到10和5.Android提交的图像的图像大小已适当减小-但IOS图像仍然很大.谁能告诉我这里是否存在已知问题?

I have reduced quality to 10 and 5. Image size of Android-submitted images are appropriately reduced- but still IOS images are large. Can anyone tell me if there is a known issue here?

API文档说,您不能降低从图库或相机胶卷中选择的图像的质量,但事实并非如此.它们是从相机加载的.

The API Docs say that you cannot reduce quality of images selected from gallery or camera roll- but these are not. They are loaded from camera.

推荐答案

这是插件在拍照后用于处理图像的代码:

This is the code the plugin uses for processing the image after taking the picture:

- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options
{
    NSData* data = nil;

    switch (options.encodingType) {
        case EncodingTypePNG:
            data = UIImagePNGRepresentation(image);
            break;
        case EncodingTypeJPEG:
        {
            if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO)){
                // use image unedited as requested , don't resize
                data = UIImageJPEGRepresentation(image, 1.0);
            } else {
                if (options.usesGeolocation) {
                    NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
                    if (controllerMetadata) {
                        self.data = data;
                        self.metadata = [[NSMutableDictionary alloc] init];

                        NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
                        if (EXIFDictionary) {
                            [self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
                        }

                        if (IsAtLeastiOSVersion(@"8.0")) {
                            [[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
                        }
                        [[self locationManager] startUpdatingLocation];
                    }
                } else {
                    data = UIImageJPEGRepresentation(image, [options.quality floatValue] / 100.0f);
                }
            }
        }
            break;
        default:
            break;
    };

    return data;
}

因此,如果options.encodingType为PNG,则图像质量不会改变.默认值为JPG,所以问题不存在.

So, if options.encodingType is PNG the image quality isn't change. The default value is JPG, so the problem isn't there.

但是请检查

if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO))

allowsEditing默认为否

targetSize.widthtargetSize.height默认为0

correctOrientation默认为否

因此满足if的所有条件,并且质量没有改变.

So all the conditions of the if are meet, and no change in quality is done.

这听起来像是个错误,因为在任何地方都没有提到它,您必须指定任何其他值才能使质量参数起作用.

It sounds like a bug as it isn't mentioned anywhere that you have to specify any other value to make the quality param work.

那么,您现在可以做什么?

So, what can you do now?

您可以在cordova jira页面 https://issues.apache.org/上提出问题.吉拉/浏览/CB 和/或您可以传递任何参数来更改默认值,并使代码跳过if并转到带有质量代码的else.更改其中任何一项都可以:

You can file an issue on the cordova jira page https://issues.apache.org/jira/browse/CB And/or you can pass any param to change the default value and make the code to skip the if and go to the else with the quality code. Changing any of this should work:

  allowEdit : true,
  targetWidth: 100,
  targetHeight: 100,
  correctOrientation: true

已经存在一个错误 https://issues.apache.org/jira/browse/CB-6190

我已解决此问题,您可以使用cordova plugin add https://github.com/apache/cordova-plugin-camera进行更改,或等待其发布到NPM

EDIT 2: I fixed the issue, you can get the changes with cordova plugin add https://github.com/apache/cordova-plugin-camera or wait until it's released to NPM

这篇关于Phonegap Build iOS camera.getPicture()质量参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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