Ios 如何翻转图像并使其在所有状态下正确显示
我需要翻转和图像,然后将其添加到按钮。我使用了以下代码Ios 如何翻转图像并使其在所有状态下正确显示,ios,objective-c,ios6,uiimage,Ios,Objective C,Ios6,Uiimage,我需要翻转和图像,然后将其添加到按钮。我使用了以下代码 UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; playButton.frame = CGRectMake(xing, ying, dx,dy); [playButton setTitle:text forState:UIControlStateNormal]; [playButton addTarget:self action:functi
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(xing, ying, dx,dy);
[playButton setTitle:text forState:UIControlStateNormal];
[playButton addTarget:self action:function forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImageNormal = [UIImage imageNamed:@"Begin-Set-Button.png"];
UIImage * other = [UIImage imageWithCGImage:buttonImageNormal.CGImage
scale:1.0 orientation: UIImageOrientationUpMirrored];
UIImage * awesome = [other stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:awesome forState:UIControlStateNormal];
它正确地显示图像,但当我单击它时,按钮会显示未折叠的图像
在尝试修复此问题时,我添加了以下代码行
[playButton setBackgroundImage:awesome forState:UIControlStateHighlighted];
然而,当我点击按钮时,它不会像我用未折叠图像创建的按钮那样使颜色变暗
如何对其进行编码,以便在使用翻转图像时,按下时翻转图像会变暗?我知道如何手动调暗图像,但我想知道是否有一种方法可以通过简单的函数调用自动调暗?我也有同样的问题,因此我搜索了一下以查看此帖子。我怀疑图像可能没有“固化”,因此镜像操作不是永久性的 签出“imageWithCGImage:”文档时显示以下内容: 讨论 此方法不缓存图像对象。您可以使用Core Graphics framework的方法创建石英图像参考 因此,除非在新的上下文中创建新的映像,否则映像的镜像不适用于事件。我还一直在研究一种方法,在任意大小的画布上绘制图像,以创建具有相同背景的按钮。您可以使用此方法创建维护CG操作的图像,例如镜像:
+ (UIImage *)imageWithCanvasSize:(CGSize)canvasSize withImage:(UIImage *)anImage
{
if (anImage.size.width > canvasSize.width ||
anImage.size.height > canvasSize.height)
{
// scale image first
anImage = [anImage scaleWithAspectToSize:canvasSize];
}
CGRect targetRect = CGRectZero;
CGFloat xOrigin = (canvasSize.width - anImage.size.width) / 2;
CGFloat yOrigin = (canvasSize.height - anImage.size.height) / 2;
targetRect.origin = CGPointMake(xOrigin, yOrigin);
targetRect.size = anImage.size;
UIGraphicsBeginImageContextWithOptions(canvasSize, NO, anImage.scale);
[anImage drawInRect:targetRect];
UIImage *canvasedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return canvasedImage;
}
- (UIImage*)scaleWithAspectToSize:(CGSize)newSize
{
CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if (self.size.width > self.size.height)
{
scaleFactor = self.size.width / self.size.height;
scaledSize.width = newSize.width;
scaledSize.height = newSize.height / scaleFactor;
}
else
{
scaleFactor = self.size.height / self.size.width;
scaledSize.height = newSize.height;
scaledSize.width = newSize.width / scaleFactor;
}
UIGraphicsBeginImageContextWithOptions(scaledSize, NO, 0.0);
CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
[self drawInRect:scaledImageRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
我也有同样的问题,所以我做了一个搜索来查看这篇文章。我怀疑图像可能没有“固化”,因此镜像操作不是永久性的 签出“imageWithCGImage:”文档时显示以下内容: 讨论 此方法不缓存图像对象。您可以使用Core Graphics framework的方法创建石英图像参考 因此,除非在新的上下文中创建新的映像,否则映像的镜像不适用于事件。我还一直在研究一种方法,在任意大小的画布上绘制图像,以创建具有相同背景的按钮。您可以使用此方法创建维护CG操作的图像,例如镜像:
+ (UIImage *)imageWithCanvasSize:(CGSize)canvasSize withImage:(UIImage *)anImage
{
if (anImage.size.width > canvasSize.width ||
anImage.size.height > canvasSize.height)
{
// scale image first
anImage = [anImage scaleWithAspectToSize:canvasSize];
}
CGRect targetRect = CGRectZero;
CGFloat xOrigin = (canvasSize.width - anImage.size.width) / 2;
CGFloat yOrigin = (canvasSize.height - anImage.size.height) / 2;
targetRect.origin = CGPointMake(xOrigin, yOrigin);
targetRect.size = anImage.size;
UIGraphicsBeginImageContextWithOptions(canvasSize, NO, anImage.scale);
[anImage drawInRect:targetRect];
UIImage *canvasedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return canvasedImage;
}
- (UIImage*)scaleWithAspectToSize:(CGSize)newSize
{
CGSize scaledSize = newSize;
float scaleFactor = 1.0;
if (self.size.width > self.size.height)
{
scaleFactor = self.size.width / self.size.height;
scaledSize.width = newSize.width;
scaledSize.height = newSize.height / scaleFactor;
}
else
{
scaleFactor = self.size.height / self.size.width;
scaledSize.height = newSize.height;
scaledSize.width = newSize.width / scaleFactor;
}
UIGraphicsBeginImageContextWithOptions(scaledSize, NO, 0.0);
CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
[self drawInRect:scaledImageRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
而不是翻转图像。您是否尝试过翻转图像视图
UIImage *img = [UIImage imageNamed:@"path/to-image.png"];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.transform = CGAffineTransformMakeRotation(M_PI); // flip the image view
[button setImage:img forState:UIControlStateNormal];
我这样做是为了保持选择状态高亮显示,而不必处理图像翻转。而不是翻转图像。您是否尝试过翻转图像视图
UIImage *img = [UIImage imageNamed:@"path/to-image.png"];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.transform = CGAffineTransformMakeRotation(M_PI); // flip the image view
[button setImage:img forState:UIControlStateNormal];
这就是我所做的,以保持选择状态高亮显示,而不必处理图像翻转。您尝试过使用setImage吗?@Joel刚刚尝试过。选择后仍然显示未折叠的图像。您是否也在IB中设置按钮背景图像,还是仅在您显示给我们的代码中设置?@Joel按钮已编码,因此没有IB。这是我制作按钮时输入的所有代码。我将在今晚查看此代码并返回给您。没有承诺,但我会看看,让你知道!你试过使用setImage吗?@Joel刚刚试过。选择后仍然显示未折叠的图像。您是否也在IB中设置按钮背景图像,还是仅在您显示给我们的代码中设置?@Joel按钮已编码,因此没有IB。这是我制作按钮时输入的所有代码。我将在今晚查看此代码并返回给您。没有承诺,但我会看看,让你知道!