将UIImage的大小调整为200x200pt / px [英] Resize UIImage to 200x200pt/px
问题描述
我一直在努力调整图片大小。
基本上我已经在stumpled:
如何缩小UIImage并使其同时变得脆弱/锐利而不是模糊?
I have been struggling resizing an image. Basically I have stumpled upon: How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?
这似乎是一个合法的解决方案,但不知何故它无法正常工作。
This seems to be a legit solution but somehow it is not working correctly.
我的应用程序使用相机胶卷中的照片。这张照片的大小应该调整到大约200x200,而宽度很重要,而不是高度。
My app works with Photos from the Camera Roll. This photos should be resized to about 200x200 whereas the width is important, not the height.
不幸的是我没有示例代码,因为我在我的愤怒中放弃了它工作解决方案,对不起。
Unfortunetly I am not having a sample code as I discarded it in my rage about non working solution, sorry.
推荐答案
这是我的代码。该图像是在宽度850像素和不是200像素:
Here is my code. The Image is in width 850 px and not 200 px:
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)
}
这篇关于将UIImage的大小调整为200x200pt / px的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!