【问题标题】:How to reduce size of the picture taken by camera in iphone 7如何缩小iphone 7中相机拍摄的照片大小
【发布时间】:2017-10-13 22:43:48
【问题描述】:

我正在开发 xamarin ios。我必须在 iphone 应用程序中实现相机功能。我已经实现了代码。但问题是,当我尝试使用网络服务上传图片时,由于图片大小,它不会上传。

所以我尝试缩放图片的大小,但在这种情况下,图片看起来会被拉伸。我希望上传的图片大小相同,可能质量最差,但高度和宽度应该是原始的。

这是我实现的代码:

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));
                }

            }); 

【问题讨论】:

    标签: ios web-services xamarin.ios


    【解决方案1】:

    不要进行缩放操作

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

    如果您不想更改大小。如果你确实想调整大小并保持纵横比,这段代码应该可以做到

    // 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;
            }
    

    如何在你的代码上使用

    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));
                    }
    
                }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多