Scaling UIImage with a UIIImagePicker#39;s CGAffineTransform and Saving to Parse SDK(使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK)
问题描述
我使用以下代码来缩放我的 UIImagePickerController
.
I use the following code to scale my UIImagePickerController
.
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
self.imagePicker.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
self.imagePicker.cameraViewTransform = scale;
然后我拍照并呈现在 UIImageView
Then I take the picture and present it in a UIImageView
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:NO completion:NULL];
_imageTaken = nil;
_imageTaken = [info objectForKey:UIImagePickerControllerEditedImage];
if(_imageTaken==nil)
{
_imageTaken = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if(_imageTaken==nil)
{
_imageTaken = [info objectForKey:UIImagePickerControllerCropRect];
}
[_imageTakenView setContentMode:UIViewContentModeScaleAspectFill];
if (_selfie) {
UIImage * flippedImage = [UIImage imageWithCGImage:_imageTaken.CGImage scale:_imageTaken.scale orientation:UIImageOrientationLeftMirrored];
_imageTaken = flippedImage;
}
_imageTakenView.image = _imageTaken;
}
到目前为止一切都很好.然后我通过将图像转换为 NSData
Everything is good so far. Then I send the image up to my database by converting it to NSData
_imageData = [[NSData alloc] init];
_imageData = UIImageJPEGRepresentation(_image, .1);
当我从服务器加载数据并将其呈现在另一个相同大小的 UIImageView
中时,我确实设置了相同的纵横比:
When I load the data back from the server and present it in another UIImageView
of the same size I do set the same aspect ratio:
[_imageViewToShow setContentMode:UIViewContentModeScaleAspectFill];
但图像看起来失真(水平挤压).关于为什么会这样的任何想法?
but the image looks distored (squished horizontally). Any ideas as to why this might be?
将 UIImage 转换为 NSData &保存
_imageData = UIImageJPEGRepresentation(_image, .1);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:_imageData];
newMessage[@"image"] = imageFile;
问题:
当我重新下载数据时,UIImage 似乎被压扁了.当我查看数据库中的图像时,它似乎很好.不知道为什么会这样……
When I redownload the data, the UIImage appears squished. When I look at the image on my database, it seems fine. Not sure why this is happening...
前后对比图:
推荐答案
在上传之前尝试标准化图像的方向.这样即使元数据丢失,图像也能正常工作
Try normalizing the image's orientation before uploading.This will allow the image to work properly even if the metadata is lost
这里有一个类别可以做到这一点:
Here is a category to do that:
UIImage+OrientationFix.h
@interface UIImage (OrientationFix)
- (UIImage*)imageByNormalizingOrientation;
@end
UIImage+OrientationFix.m
@implementation UIImage (OrientationFix)
- (UIImage*)imageByNormalizingOrientation {
if (self.imageOrientation == UIImageOrientationUp)
return self;
CGSize size = self.size;
UIGraphicsBeginImageContextWithOptions(size, NO, self.scale);
[self drawInRect:(CGRect){{0, 0}, size}];
UIImage* normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}
@end
您只需拨打电话:
_imageTaken = [flippedImage normalizedImage];
这篇关于使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UIIImagePicker 的 CGAffineTransform 缩放 UIImage 并保存到 Parse SDK
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01