按钮通过 ivars 方法获取 EXC_BAD_ACCESS

发布于 2024-12-13 20:14:07 字数 1549 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不爱素颜 2024-12-20 20:14:07

不要在createAnimation方法中释放animationImage。您还应该使用 CGImageRelease 清理 CGImageRef 的内存

Do not release animationImage in createAnimation method. You also should clean memory for CGImageRef using CGImageRelease

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文