按钮通过 ivars 方法获取 EXC_BAD_ACCESS
我是 obj-c 的新手,所以我无法理解其中的一些逻辑。我想了解我的代码和应用程序逻辑。我的应用程序是对 UIImageView 动画的简单示例进行修改:这是 .h (在基于标准视图的模板中)
#import <UIKit/UIKit.h>
@interface ImageAnimatorViewController : UIViewController {
UIImageView *animImage;
}
@property (nonatomic, retain) IBOutlet UIImageView *animImage;
- (IBAction)startAnimation;
- (NSArray*)creatAnimation:(NSString*)fileName;
@end
这是 .m (没有标准 dealloc 和 viewDidUnload)
@synthesize animImage;
- (IBAction)startAnimation{
[animImage startAnimating];
}
- (void)viewDidLoad{
[super viewDidLoad];
animImage.animationImages = [self creatAnimation:@"Images.jpg"];
animImage.animationDuration = 1.0f;
animImage.animationRepeatCount = 0;
}
- (NSArray*)creatAnimation:(NSString*)fileName{
UIImage *image = [UIImage imageNamed:fileName];
NSMutableArray *animationImages = [NSMutableArray array];
for (int i = 0; i<8; i++) {
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage,
CGRectMake(i*600.0f, 0.0f, 600.0f, 262.0f));
UIImage *animationImage = [UIImage imageWithCGImage:imageRef];
[animationImages addObject:animationImage];
[animationImage release];
}
return animationImages;
}
另外,我还有带有 imageview 和按钮的 xib,但是当我按下 b 时,出现崩溃和控制台中的 EXC_BAD_ACCESS。在IBAction中我知道imageView没有animationImages,但为什么呢?并且有奇怪的引用计数= 3,因为在viewDidLoad中有2(为什么是2?)。我添加自我和自我-> viewDidLoad 中的前缀,但没有结果。 AnimationImages 是(复制)属性,因此在自动释放池用animationImages 耗尽后图像会保存。我对 app.10x 的这种行为感到非常惊讶!
I'm neophyte in obj-c, so I cant understand some of this logic. I want to understand my code and app logic. My app is modification of easy example with animation of UIImageView: this's .h (in standart view-based template)
#import <UIKit/UIKit.h>
@interface ImageAnimatorViewController : UIViewController {
UIImageView *animImage;
}
@property (nonatomic, retain) IBOutlet UIImageView *animImage;
- (IBAction)startAnimation;
- (NSArray*)creatAnimation:(NSString*)fileName;
@end
this's .m (without standart dealloc and viewDidUnload)
@synthesize animImage;
- (IBAction)startAnimation{
[animImage startAnimating];
}
- (void)viewDidLoad{
[super viewDidLoad];
animImage.animationImages = [self creatAnimation:@"Images.jpg"];
animImage.animationDuration = 1.0f;
animImage.animationRepeatCount = 0;
}
- (NSArray*)creatAnimation:(NSString*)fileName{
UIImage *image = [UIImage imageNamed:fileName];
NSMutableArray *animationImages = [NSMutableArray array];
for (int i = 0; i<8; i++) {
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage,
CGRectMake(i*600.0f, 0.0f, 600.0f, 262.0f));
UIImage *animationImage = [UIImage imageWithCGImage:imageRef];
[animationImages addObject:animationImage];
[animationImage release];
}
return animationImages;
}
Also i've xib with imageview and button, but when i pressed b there's crash and EXC_BAD_ACCESS in console. In IBAction I understand imageView hasn't animationImages, but why? and has strange reference count = 3,because in viewDidLoad has 2(and why 2?). I add self and self-> prefix in viewDidLoad, but it's no result. animationImages is (copy) property,so images saves after autorelease pool is drained with animationImages. I'm very surprised about this behavior of app.10x!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在createAnimation方法中释放animationImage。您还应该使用 CGImageRelease 清理 CGImageRef 的内存
Do not release animationImage in createAnimation method. You also should clean memory for CGImageRef using CGImageRelease