- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
所以我一直在试图弄清楚我做错了一段时间,但我无法弄清楚。我想要完成的是:
UIImagePickerController
拍照UIButton
中显示该图像出于某种原因,每次我拍摄照片时,它都会在 UIButton
中变形,并且看起来好像裁剪工作不正常。这就是我所做的。在 didFinishPickingMediaWithInfo
方法中,我有以下代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Copy the image to the userImage variable
[picker dismissModalViewControllerAnimated:YES];
userImage = nil;
//Rotate & resize image
userImage = [self resizeAndRotatePhoto:(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]];
NSLog(@"Userimage size, width: %f , height: %f", userImage.size.width , userImage.size.height);
//Update the image form field with the image and set the image in the controller
NSLog(@"Button size is height: %f , width: %f" , userImageAvatarButton.frame.size.height , userImageAvatarButton.frame.size.width);
[userImageAvatarButton.layer setMasksToBounds:YES];
[userImageAvatarButton.layer setCornerRadius:3.0];
[userImageAvatarButton setImage:userImage forState:UIControlStateNormal];
}
我将暂时包含 resizeAndRotatePhoto 方法,但结果如下所示。另外,在上面的代码中,@property (strong) (UIImage *)userImage;
是在 ViewController 的头文件中定义的。日志输出还导致:
2012-05-07 17:38:07.995 NewApp[10666:707] Userimage size, width: 1936.000000 , height: 1936.000000
2012-05-07 17:38:08.000 NewApp[10666:707] Button size is height: 60.000000 , width: 60.000000
正如您在下图中看到的,它最终变形了。
关于resizeAndRotate方法,这里是:
- (UIImage *)resizeAndRotatePhoto:(UIImage *)source
{
if( source.imageOrientation == UIImageOrientationRight )
{
source = [self rotateImage:source byDegrees:90];
}
if( userImage.imageOrientation == UIImageOrientationLeft)
{
source = [self rotateImage:source byDegrees:-90];
}
CGFloat x,y;
CGFloat size;
if( source.size.width > source.size.height ){
size = source.size.height;
x = (source.size.width - source.size.height)/2;
y = 0;
}
else {
size = source.size.width;
x = 0;
y = (source.size.height - source.size.width)/2;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([source CGImage], CGRectMake(x,y,size,size) );
return [UIImage imageWithCGImage:imageRef];
}
在这一点上,我不知道如何让这张图片不失真地显示出来。尽管系统说图像确实是正方形,但它似乎裁剪错误并显示错误。
最佳答案
我出于其他原因放弃了 vocaro.com 解决方案(该解决方案在使用非典型图像格式(例如 CMYK)时会崩溃)。
无论如何,我现在使用以下 UIImage
类别的 imageByScalingAspectFillSize
方法制作我的方形缩略图。
顺便说一下,这会自动应用设备主屏幕的比例(例如,UIButton
按钮在 Retina 设备上是 60x60 点,这将应用 2x(或 3x)的比例),即 120x120 或 180x180 的图像)。
UIImage+SimpleResize.h:
/* UIImage+SimpleResize.h
*
* Modified by Robert Ryan on 5/19/11.
*/
@import UIKit;
/** Image resizing category.
*
* Modified by Robert Ryan on 5/19/11.
*
* Inspired by http://ofcodeandmen.poltras.com/2008/10/30/undocumented-uiimage-resizing/
* but adjusted to support AspectFill and AspectFit modes.
*/
@interface UIImage (SimpleResize)
/** Resize the image to be the required size, stretching it as needed.
*
* @param size The new size of the image.
* @param contentMode The `UIViewContentMode` to be applied when resizing image.
* Either `UIViewContentModeScaleToFill`, `UIViewContentModeScaleAspectFill`, or
* `UIViewContentModeScaleAspectFit`.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode;
/** Resize the image to be the required size, stretching it as needed.
*
* @param size The new size of the image.
* @param contentMode The `UIViewContentMode` to be applied when resizing image.
* Either `UIViewContentModeScaleToFill`, `UIViewContentModeScaleAspectFill`, or
* `UIViewContentModeScaleAspectFit`.
* @param scale The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode scale:(CGFloat)scale;
/** Crop the image to be the required size.
*
* @param bounds The bounds to which the new image should be cropped.
*
* @return Cropped `UIImage`.
*/
- (UIImage * _Nullable)imageByCroppingToBounds:(CGRect)bounds;
/** Crop the image to be the required size.
*
* @param bounds The bounds to which the new image should be cropped.
* @param scale The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
*
* @return Cropped `UIImage`.
*/
- (UIImage * _Nullable)imageByCroppingToBounds:(CGRect)bounds scale:(CGFloat)scale;
/** Resize the image to fill the rectange of the specified size, preserving the aspect ratio, trimming if needed.
*
* @param size The new size of the image.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingAspectFillSize:(CGSize)size;
/** Resize the image to fill the rectange of the specified size, preserving the aspect ratio, trimming if needed.
*
* @param size The new size of the image.
* @param scale The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingAspectFillSize:(CGSize)size scale:(CGFloat)scale;
/** Resize the image to be the required size, stretching it as needed.
*
* @param size The new size of the image.
*
* @return Resized `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingToFillSize:(CGSize)size;
/** Resize the image to be the required size, stretching it as needed.
*
* @param size The new size of the image.
* @param scale The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
*
* @return Resized `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingToFillSize:(CGSize)size scale:(CGFloat)scale;
/** Resize the image to fit within the required size, preserving the aspect ratio, with no trimming taking place.
*
* @param size The new size of the image.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingAspectFitSize:(CGSize)size;
/** Resize the image to fit within the required size, preserving the aspect ratio, with no trimming taking place.
*
* @param size The new size of the image.
* @param scale The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
*
* @return Return `UIImage` of resized image.
*/
- (UIImage * _Nullable)imageByScalingAspectFitSize:(CGSize)size scale:(CGFloat)scale;
@end
UIImage+SimpleResize.m:
// UIImage+SimpleResize.m
//
// Created by Robert Ryan on 5/19/11.
#import "UIImage+SimpleResize.h"
@implementation UIImage (SimpleResize)
- (UIImage *)imageByScalingToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode {
return [self imageByScalingToSize:size contentMode:contentMode scale:0];
}
- (UIImage *)imageByScalingToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode scale:(CGFloat)scale {
if (contentMode == UIViewContentModeScaleToFill) {
return [self imageByScalingToFillSize:size];
}
else if ((contentMode == UIViewContentModeScaleAspectFill) ||
(contentMode == UIViewContentModeScaleAspectFit)) {
CGFloat horizontalRatio = self.size.width / size.width;
CGFloat verticalRatio = self.size.height / size.height;
CGFloat ratio;
if (contentMode == UIViewContentModeScaleAspectFill)
ratio = MIN(horizontalRatio, verticalRatio);
else
ratio = MAX(horizontalRatio, verticalRatio);
CGSize sizeForAspectScale = CGSizeMake(self.size.width / ratio, self.size.height / ratio);
UIImage *image = [self imageByScalingToFillSize:sizeForAspectScale scale:scale];
// if we're doing aspect fill, then the image still needs to be cropped
if (contentMode == UIViewContentModeScaleAspectFill) {
CGRect subRect = CGRectMake(floor((sizeForAspectScale.width - size.width) / 2.0),
floor((sizeForAspectScale.height - size.height) / 2.0),
size.width,
size.height);
image = [image imageByCroppingToBounds:subRect];
}
return image;
}
return nil;
}
- (UIImage *)imageByCroppingToBounds:(CGRect)bounds {
return [self imageByCroppingToBounds:bounds scale:0];
}
- (UIImage *)imageByCroppingToBounds:(CGRect)bounds scale:(CGFloat)scale {
if (scale == 0) {
scale = [[UIScreen mainScreen] scale];
}
CGRect rect = CGRectMake(bounds.origin.x * scale, bounds.origin.y * scale, bounds.size.width * scale, bounds.size.height * scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return croppedImage;
}
- (UIImage *)imageByScalingToFillSize:(CGSize)size {
return [self imageByScalingToFillSize:size scale:0];
}
- (UIImage *)imageByScalingToFillSize:(CGSize)size scale:(CGFloat)scale {
UIGraphicsBeginImageContextWithOptions(size, false, scale);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)imageByScalingAspectFillSize:(CGSize)size {
return [self imageByScalingAspectFillSize:size scale:0];
}
- (UIImage *)imageByScalingAspectFillSize:(CGSize)size scale:(CGFloat)scale {
return [self imageByScalingToSize:size contentMode:UIViewContentModeScaleAspectFill scale:scale];
}
- (UIImage *)imageByScalingAspectFitSize:(CGSize)size {
return [self imageByScalingAspectFitSize:size scale:0];
}
- (UIImage *)imageByScalingAspectFitSize:(CGSize)size scale:(CGFloat)scale {
return [self imageByScalingToSize:size contentMode:UIViewContentModeScaleAspectFit scale:scale];
}
@end
关于objective-c - UIImage 调整大小无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10491080/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!