我一直在努力调整图像的大小. 基本上我偶然发现了: How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?

这似乎是一个合法的解决方案,但不知何故它不能正常工作.

我的应用程序可以处理来自Camera Roll的照片.这张照片的大小应该调整到200x200左右,但重要的是宽度,而不是高度.

不幸的是,我没有一个示例代码,因为我对无法工作的解决方案感到愤怒,所以放弃了它,对不起.

推荐答案

这是我的密码.图像的宽度为850像素,而不是200像素:

 func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {

    let scale = newWidth / image.size.width
    let newHeight = image.size.height * scale
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
    image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}


@IBAction func chooseImage(sender: AnyObject) {


    var myPickerController = UIImagePickerController()
    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    myPickerController.delegate = self;
    self.presentViewController(myPickerController, animated: true, completion: nil)


}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])

{
    var imagenow = info[UIImagePickerControllerOriginalImage] as? UIImage

    imageImage.image = resizeImage(imagenow!, newWidth: 200)



    pimg2 = imageImage.image!

    cidnew2 = textFieldCID!.text!
    pname2 = textFieldName!.text
    pmanu2 = textFieldMan!.text
    pnick2 = textFieldNick!.text
    podate2 = textFieldPODate!.text
    pno2 = textFieldArtNo!.text



    self.dismissViewControllerAnimated(true, completion: nil)

}

Ios相关问答推荐

带外部笔划的圆形进度视图

如何创建自定义组件来接受(和传递)所有可能的Textfield参数?

DriverKit驱动程序中可能存在IOBufferMemoyDescriptor泄漏

无法在所有窗口上方显示图像

从后台线程调用DispatchObject的方法安全吗?

在嵌套的DISPATCH_AFTER块中正确使用strong自/弱自

Swift locationManager.requestWhenInUseAuthorization() 不提示用户

如何在 Flutter 应用程序中测试来自 App/Play Store 的移动深层链接?

长按 SwiftUI 更改项目的顺序

视图之间的 SwiftUI 过渡,没有过渡

如何在 SwiftUI 的列表中使用带有 swipeActions 的 NavigationLink

iOS,Swift 5,AppDelegate open url / .onOpenUrl - 如何找到调用我的自定义方案的调用应用程序

iOS 中的 [Class new] 和 [[Class alloc] init] 有什么区别?

iPhone iOS 无法正确显示 box-shadow

为什么 Safari 在使用 iOS 6 设备进行远程调试时显示No Inspectable Applications?

在 Swift 中禁用向后滑动手势

在滚动视图中使用动态大小的控制器调整容器视图的大小

如何找出用于构建 *.ipa 文件的配置文件?

无法加载从带有标识符的包中的笔尖引用的图像

NSURLSession:如何增加 URL 请求的超时时间?