我在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编程和编码的新手,请原谅我愚蠢的代码,错误和糟糕的英语:)
谢谢!