1

我在 OS X Mavericks 上安装了 XCode 5.0.1。现在我将尝试描述一种情况。我在情节提要中有一个视图,其中有 384 个 UIImageViews。所有这些都集中在 IBOutletCollection 中。这个想法包括:在一个循环中,以随机顺序排列每个 UIImageView 一个或其他图像,并在某个方向上旋转(此外,以随机顺序)。当我在模拟器中运行我的应用程序时,它会立即关闭并给出一个错误,如在标题中。请帮助我理解发生了什么问题。这是.h文件的源代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    int i, irn, rn;

    NSArray *clonedArray;

}

@property (nonatomic, retain) IBOutletCollection(UIImageView) NSArray *imageCollection;

-(void)didSwipe:(UISwipeGestureRecognizer *)sender;

@end

和 .m 文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)setImageViewsImages {

    [self createRandoms];
    for (i = 0; i < 384; i++) {

        if (irn == 1) {

            UIImageView *currentImage = [UIImageView alloc];
            [currentImage setImage:[UIImage imageNamed:@"1.png"]];

            if (rn == 1) {
                CGAffineTransform imageRotation = CGAffineTransformMakeRotation(0);
                currentImage.transform = imageRotation;
                [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                NSLog (@"image set");

            } else {
                switch (rn) {

                    case 2: {
                        CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 0.5);
                        currentImage.transform = imageRotation;
                        [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                        [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                        NSLog (@"image set"); }
                    break;

                    case 3: {
                        CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI);
                        currentImage.transform = imageRotation;
                        [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                        [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                        NSLog (@"image set"); }
                    break;

                    case 4: {
                        CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 1.5);
                        currentImage.transform = imageRotation;
                        [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                        [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                        NSLog (@"image set"); }
                    break;
                }

            }

            currentImage.image = nil;

        }

        if (irn == 2) {

            UIImageView *currentImage = [UIImageView alloc];
            [currentImage setImage:[UIImage imageNamed:@"2.png"]];

            if (rn == 1) {
                CGAffineTransform imageRotation = CGAffineTransformMakeRotation(0);
                currentImage.transform = imageRotation;
                [[self.imageCollection objectAtIndex:i] setObject:currentImage atIndex:i];
                NSLog (@"image set");

            } else {
                switch (rn) {

                    case 2: {
                        CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 0.5);
                        currentImage.transform = imageRotation;
                        [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                        [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                        NSLog (@"image set"); }
                    break;

                    case 3: {
                        CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI);
                        currentImage.transform = imageRotation;
                        [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                        [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                        NSLog (@"image set"); }
                    break;

                    case 4: {
                        CGAffineTransform imageRotation = CGAffineTransformMakeRotation(M_PI * 1.5);
                        currentImage.transform = imageRotation;
                        [[clonedArray objectAtIndex:i] setObject:currentImage atIndex:i];
                        [[self.imageCollection objectAtIndex:i] setObject:[clonedArray objectAtIndex:i] atIndex:i];
                        NSLog (@"image set"); }
                    break;
                }

            }

            currentImage.image = nil;

        }

    }

}

-(void)createRandoms {

    rn = (arc4random() %4) + 1;
    irn = (arc4random() %2) + 1;

}

-(void)viewDidLoad {

    [super viewDidLoad];

    [self setImageViewsImages];
    [self createRandoms];

    clonedArray = [[NSArray alloc] initWithArray:self.imageCollection];

    UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                  action:@selector(didSwipe:)];
    upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:upSwipe];

    UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(didSwipe:)];
    downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:downSwipe];

}

-(void)didSwipe:(UISwipeGestureRecognizer *)sender {

    UISwipeGestureRecognizerDirection direction = sender.direction;

    switch (direction) {

        case UISwipeGestureRecognizerDirectionUp:

            UIGraphicsBeginImageContext(self.view.bounds.size);

            [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

            UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();

            UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);

        break;



        case UISwipeGestureRecognizerDirectionDown:
            [self createRandoms];
            [self setImageViewsImages];
        break;


        case UISwipeGestureRecognizerDirectionRight:

        break;

        case UISwipeGestureRecognizerDirectionLeft:

        break;

    }
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error != NULL) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Picture cannot be saved. \nTry again."
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Close", nil];
        [alert show];

    }

    else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                        message:@"Picture saved successfully."
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Close", nil];
        [alert show];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

我是 Objective-C 编程和编码的新手,所以,请原谅我的愚蠢代码、错误和糟糕的英语:)

谢谢!

4

0 回答 0