0

In my project, I have my image files stored in jpeg format. The code below "works" in that it doesn't crash, but my images don't flip when they're supposed to. I threw a breakpoint in to ensure that the code is running--it is, but the images that are displayed aren't flipped. I'm guessing I'm missing something basic, but it's not jumping out at me.

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;
        }
    }

I followed this post, but nothing's happening.

How to flip UIImage horizontally?

I added #import <QuartzCore/QuartzCore.h> to my implementation file. Aside from that, I'm out of ideas re: how to flip an image horizontally.

UPDATE: Much easier solution

Rather than flipping the UIImage, I flipped the UIImageView. The code above I used to create my imageArray now looks like this:

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;
        }
    }

and the code I used to flip my UIImageView is in the answer below.

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180

1 Answers1

2

Problem solved. Rather than flipping the UIImage, I flipped the UIImageView as follows:

self.myImageView.transform = CGAffineTransformMakeScale(-1, 1);
Adrian
  • 16,233
  • 18
  • 112
  • 180