一尘不染

将UIImage调整为200x200pt / px

swift

我一直在努力调整图像的大小。基本上,我很费解:如何缩小UIImage并同时使其酥脆/清晰而不是模糊?

这似乎是一个合法的解决方案,但是以某种方式无法正常工作。

我的应用程序可使用相机胶卷中的照片。这些照片应调整为200x200左右,而宽度很重要,而不是高度。

不幸的是,我没有示例代码,因为在无法解决问题的愤怒中我将其丢弃。


阅读 328

收藏
2020-07-07

共1个答案

一尘不染

这是我的代码。图片的宽度为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)

}
2020-07-07