我在做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));
}
}); 发布于 2017-05-15 08:48:44
不要执行规模操作
var image =photo.Scale(新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));
}
}); https://stackoverflow.com/questions/43974553
复制相似问题