在 UIImageView 中水平翻转 UIImage
Flipping a UIImage horizontally in UIImageView
在我的项目中,我的图像文件以 jpeg 格式存储。 "works" 下面的代码不会崩溃,但我的图像不会按预期翻转。我在其中设置了一个断点以确保代码是 运行--它是,但显示的图像没有翻转。我猜我错过了一些基本的东西,但它并没有跳出来。
if (self.isTimer == YES) {
// this is where images get dumped if they exist
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.currentItem.photo != nil; imageNumber++) {
NSString *fileName = [NSString stringWithFormat:@"%@-%d", self.currentItem.photo, imageNumber];
if ([UIImage imageNamed:fileName]) {
UIImage *image = [UIImage imageNamed:fileName];
// test whether count is even or odd
if (self.currentItem.sideMultiplier.intValue == 1) {
// if there's only one side to be done, dump unflipped images in array
[imageArray addObject:image];
NSLog(@"regular image added to array");
} else {
// if there are 2 sides, determine if it's even or odd side
if (self.stretchSideMultiplierCount % 2 == 1) {
// add a unflipped image to the array
[imageArray addObject:image];
NSLog(@"regular image added to array");
} else {
// ** I want to add a FLIPPED IMAGE here
UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
[imageArray addObject:flippedImage];
NSLog(@"flipped image added to array");
}
}
NSLog(@"%@ added to imageArray", fileName);
} else {
break;
}
}
我关注了这个 post,但没有任何反应。
How to flip UIImage horizontally?
我将 #import <QuartzCore/QuartzCore.h>
添加到我的实施文件中。除此之外,我不知道如何水平翻转图像。
更新:更简单的解决方案
我没有翻转 UIImage
,而是翻转了 UIImageView
。上面我用来创建 imageArray
的代码现在看起来像这样:
if (self.isTimer == YES) {
// this is where images get dumped if they exist
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.currentItem.photo != nil; imageNumber++) {
NSString *fileName = [NSString stringWithFormat:@"%@-%d", self.currentItem.photo, imageNumber];
if ([UIImage imageNamed:fileName]) {
UIImage *image = [UIImage imageNamed:fileName];
[imageArray addObject:image];
NSLog(@"image added to array");
NSLog(@"%@ added to imageArray", fileName);
} else {
break;
}
}
我用来翻转 UIImageView
的代码在下面的答案中。
问题已解决。我没有翻转 UIImage
,而是翻转了 UIImageView
,如下所示:
self.myImageView.transform = CGAffineTransformMakeScale(-1, 1);
在我的项目中,我的图像文件以 jpeg 格式存储。 "works" 下面的代码不会崩溃,但我的图像不会按预期翻转。我在其中设置了一个断点以确保代码是 运行--它是,但显示的图像没有翻转。我猜我错过了一些基本的东西,但它并没有跳出来。
if (self.isTimer == YES) {
// this is where images get dumped if they exist
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.currentItem.photo != nil; imageNumber++) {
NSString *fileName = [NSString stringWithFormat:@"%@-%d", self.currentItem.photo, imageNumber];
if ([UIImage imageNamed:fileName]) {
UIImage *image = [UIImage imageNamed:fileName];
// test whether count is even or odd
if (self.currentItem.sideMultiplier.intValue == 1) {
// if there's only one side to be done, dump unflipped images in array
[imageArray addObject:image];
NSLog(@"regular image added to array");
} else {
// if there are 2 sides, determine if it's even or odd side
if (self.stretchSideMultiplierCount % 2 == 1) {
// add a unflipped image to the array
[imageArray addObject:image];
NSLog(@"regular image added to array");
} else {
// ** I want to add a FLIPPED IMAGE here
UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
[imageArray addObject:flippedImage];
NSLog(@"flipped image added to array");
}
}
NSLog(@"%@ added to imageArray", fileName);
} else {
break;
}
}
我关注了这个 post,但没有任何反应。
How to flip UIImage horizontally?
我将 #import <QuartzCore/QuartzCore.h>
添加到我的实施文件中。除此之外,我不知道如何水平翻转图像。
更新:更简单的解决方案
我没有翻转 UIImage
,而是翻转了 UIImageView
。上面我用来创建 imageArray
的代码现在看起来像这样:
if (self.isTimer == YES) {
// this is where images get dumped if they exist
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.currentItem.photo != nil; imageNumber++) {
NSString *fileName = [NSString stringWithFormat:@"%@-%d", self.currentItem.photo, imageNumber];
if ([UIImage imageNamed:fileName]) {
UIImage *image = [UIImage imageNamed:fileName];
[imageArray addObject:image];
NSLog(@"image added to array");
NSLog(@"%@ added to imageArray", fileName);
} else {
break;
}
}
我用来翻转 UIImageView
的代码在下面的答案中。
问题已解决。我没有翻转 UIImage
,而是翻转了 UIImageView
,如下所示:
self.myImageView.transform = CGAffineTransformMakeScale(-1, 1);