简体   繁体   English

如何缩小iPhone 7中相机拍摄的照片的大小

[英]How to reduce size of the picture taken by camera in iphone 7

I am working on xamarin ios. 我正在使用Xamarin iOS。 I have to implement camera functionality in iphone app. 我必须在iPhone应用程序中实现相机功能。 I have implemted the code. 我已经破坏了代码。 But the problem is that when I try to upload the picture with webservices, it doesn't upload due to size of the image. 但是问题是,当我尝试使用网络服务上传图片时,由于图片的大小而无法上传。

So I tried to scale the size of picture but in that case the image is looking strethced. 因此,我尝试缩放图片的大小,但在那种情况下,图片看起来显得很粗糙。 I want that image should be uploaded with same size, may be with worst quality but height and width should be original. 我希望该图片应该以相同的大小上传,但质量可能最差,但高度和宽度应该是原始的。

Here is the code I implemented: 这是我实现的代码:

Camera.TakePicture(this, (obj) =>
            {
                var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;
                var image = photo.Scale(new CGSize(1280, 720));
                Byte[] myByteArray;
                using (NSData imageData = image.AsJPEG(0.0f))
                {

                    myByteArray = new Byte[imageData.Length];
                    System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
                }

            }); 

Dont perform the scale operation 不执行缩放操作

var image = photo.Scale(new CGSize(1280, 720)); var image = photo.Scale(new CGSize(1280,720));

if you DONT want to change the size. 如果您不想更改大小。 If you do want to resize and also keep the aspect ratio, this code sniped should do it 如果您确实想调整大小并同时保持宽高比,则可以通过截短此代码来做到这一点

// Resize the image to be contained within a maximum width and height, keeping aspect ratio
        public UIImage MaxResizeImage (UIImage sourceImage, float maxWidth, float maxHeight)
        {
            var sourceSize = sourceImage.Size;
            var maxResizeFactor = Math.Max (maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
            if (maxResizeFactor > 1) return sourceImage;
            var width = maxResizeFactor * sourceSize.Width;
            var height = maxResizeFactor * sourceSize.Height;
            UIGraphics.BeginImageContext (new SizeF ((float)width, (float)height));
            sourceImage.Draw (new RectangleF (0, 0, (float)width, (float)height));
            var resultImage = UIGraphics.GetImageFromCurrentImageContext ();
            UIGraphics.EndImageContext ();
            return resultImage;
        }

How to use on your code 如何在您的代码上使用

Camera.TakePicture(this, (obj) =>
            {
                var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;
                var image = MaxResizeImage (photo,1280, 720);
                Byte[] myByteArray;
                using (NSData imageData = image.AsJPEG(0.0f))
                {

                    myByteArray = new Byte[imageData.Length];
                    System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
                }

            }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM