gpt4 book ai didi

android - 需要帮助将进程从 Java(android) 转换为 IOS Swift

转载 作者:行者123 更新时间:2023-11-28 13:16:34 27 4
gpt4 key购买 nike

背景:我在 Android 应用程序中有一个进程调整图像大小,使用位图中 30% 的 JPEG 压缩,并返回一个字节数组,我在其中转换为 base64Encoded 字符串。如果可能,我需要将此类功能移植到 IOS Swift。我正在经历网络上用于图像处理的大量方法的信息过载,我需要更多指导。这是我的安卓代码:

Bitmap bmp = null;
Bitmap scaledBitmap = null;
ByteArrayOutputStream baos = null;

try
{
bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

//if the bitmap is smaller than 1600 wide, scale it up while preserving aspect ratio
if(bmp.getWidth() < 1600) {
int originalHeight = bmp.getHeight();
int originalWidth = bmp.getWidth();

scaledBitmap = Bitmap.createScaledBitmap(bmp, 1600,
originalHeight*1600/originalWidth, true);

bmp = scaledBitmap;
scaledBitmap = null;
}

baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos); // 30% compression
image = baos.toByteArray();
}
//catch stuff after this

到目前为止,这是我的 IOS Swift 代码:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
picker.dismissViewControllerAnimated(true, completion: nil)

let image = info[UIImagePickerControllerOriginalImage] as? UIImage

self.imgCheckFront.image = info[UIImagePickerControllerOriginalImage] as? UIImage

let imageData = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage] as? UIImage, 30.0)

let base64String = imageData.base64EncodedStringWithOptions(.allZeros)

}

}

我认为这与我的 Android 流程有很大不同。我在 IOS 代码中创建的结果 base64String 的大小太大了。

最佳答案

抱歉,这是一个愚蠢的错误。这个:

let imageData = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage] as? UIImage, 30.0)

需要是这样的:

let imageData = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage] as? UIImage, 0.3)

关于android - 需要帮助将进程从 Java(android) 转换为 IOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com