这个问题在这里已有答案:
我正在使用UIImagePickerController从我的库中选择一个图像并将其上传到Parse . 我怎样才能调整图像的高度?我希望图像保持它的纵横比,但我不希望高度高于1000px .
现在,我正在使用以下代码将宽度和高度调整为固定数字:
ViewController.h
- (UIImage *)resizeImage:(UIImage *)image toWidth:(float)width andHeight:(float)height;
ViewController.h
- (UIImage *)resizeImage:(UIImage *)image toWidth:(float)width andHeight:(float)height {
CGSize newSize = CGSizeMake(width, height);
CGRect newRectangle = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(newSize);
[self.image drawInRect:newRectangle];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
- (IBAction)createProduct:(id)sender {
UIImage *newImage = [self resizeImage:self.image toWidth:750.0f andHeight:1000.0f];
NSData *imageData = UIImagePNGRepresentation(newImage);
PFFile *imageFile = [PFFile fileWithName:@"image.jpg" data:imageData];
}
谢谢 .
2 回答
您将必须获得原始尺寸的纵横比,并将其与您的新高度相乘
伪代码