2009-04-07 50 views
3

我在使用UIImageView动画时遇到了一些问题。我创建了三个包含图像的NSArray对象,并创建了三个采用UIImageView并将新动画分配给它的方法。它第一次调用三种方法之一,但是再次做它会使模拟器和设备崩溃。有任何想法吗?iPhone UIImageView动画效果

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

// set the anims 
slowAnimation = [NSArray arrayWithObjects: 
        [UIImage imageNamed:@"image1.jpg"], 
        [UIImage imageNamed:@"image2.jpg"], 
           nil]; 
defaultAnimation = [NSArray arrayWithObjects: 
        [UIImage imageNamed:@"image3.jpg"], 
        [UIImage imageNamed:@"image4.jpg"], 
         nil]; 
fastAnimation = [NSArray arrayWithObjects: 
        [UIImage imageNamed:@"image5.jpg"], 
        [UIImage imageNamed:@"image6.jpg"], 
        nil]; 

// load the default speed - this works once, but when applied a second time it crashes the simulator and device 
[self setDefaultAnim:myUIImageViewObject]; 

[super viewDidLoad]; 
} 


// animation switcher 
-(void)setSlowAnim:(UIImageView *)imageView 
{ 
    [imageView stopAnimating]; 
    imageView.animationImages = slowAnimation; 
    imageView.animationDuration = 0.4; 
    [imageView startAnimating]; 
} 
-(void)setDefaultAnim:(UIImageView *)imageView 
{ 
    [imageView stopAnimating]; 
    imageView.animationImages = defaultAnimation; 
    imageView.animationDuration = 0.4; 
     [imageView startAnimating]; 
} 
-(void)setFastAnim:(UIImageView *)imageView 
{ 
    [imageView stopAnimating]; 
    imageView.animationImages = fastAnimation; 
    imageView.animationDuration = 0.4; 
    [imageView startAnimating]; 
} 

错误日志:

[Session started at 2009-04-06 22:46:54 +0100.] 
Loading program into debugger… 
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008) 
Copyright 2004 Free Software Foundation, Inc. 
GDB is free software, covered by the GNU General Public License, and you are 
welcome to change it and/or distribute copies of it under certain conditions. 
Type "show copying" to see the conditions. 
There is absolutely no warranty for GDB. Type "show warranty" for details. 
This GDB was configured as "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found). 
warning: Unable to read symbols from "UIKit" (not yet mapped into memory). 
warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found). 
warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory). 
Program loaded. 
sharedlibrary apply-load-rules all 
Attaching to program: `/Users/hello/Library/Application Support/iPhone Simulator/User/Applications/6DB7C45D-1A26-4775-9AE3-C30F3EBC9F83/PlateSpinner.app/PlateSpinner', process 345. 
kill 
quit 

The Debugger has exited with status 0. 

回答

8

我认为你需要retain您在viewDidLoad创建三个数组 - 你永远不呼吁他们alloc,所以你没有所有权,直到你打电话retain

+0

完美,谢谢:] – antonmills 2009-04-07 21:19:22

3

而不只是呼吁保留使用initWithObjects消息:

slowAnimation = [[NSArray alloc] initWithObjects: 
            [UIImage imageNamed:@"image1.jpg"], 
            [UIImage imageNamed:@"image2.jpg"], 
            nil]; 
0

如果你做了什么酒廊建议,不过,请注意,您将需要稍后释放slowAnimation对象。既然你已经分配了它,它将有一个保留计数为1,直到有人告诉运行时否则。