gpt4 book ai didi

ios - 内存管理 iOS 开发应用程序在一些细节项目后不起作用

转载 作者:行者123 更新时间:2023-11-28 17:32:23 25 4
gpt4 key购买 nike

我正在处理一个带有 tableView Controller 的项目,详细 View 包含 CMMotionManager。当我打开 5 或 6 个 detailViews 时一切顺利,但一段时间后应用程序变慢并最终崩溃。

在仪器上,唯一的泄漏是在 main.m 上,而且我必须说我正在使用 ARC,我无法释放或重新分配实例。

代码如下:首先是 TableView :

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"Movement";//Master View Controller title bar
UIImage *image = [UIImage imageNamed:@"jg_navibar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

//Init the array with data
bodypartsMutableArray = [NSMutableArray arrayWithCapacity:26];

BodypartData *part1 = [[BodypartData alloc] init];
part1.bodypartname = @"Shoulder";
part1.movementname = @"Flexion";
part1.fullimageStartingPosition=[UIImage imageNamed:@"2_shoulder_flexion_end_position.jpg"];
part1.fullimageEndedPosition=[UIImage imageNamed:@"2_shoulder_flexion_end_position.jpg"];
part1.thumbimage=[UIImage imageNamed:@"1_shoulder_flexion_landmarks_thumb.jpg"];
[bodypartsMutableArray addObject:part1];

.........
}

然后是单元格:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyBasicCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
BodypartData *part = [self.bodypartsMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text =[NSString stringWithFormat:part.movementname];
cell.detailTextLabel.text =[NSString stringWithFormat:part.bodypartname];
cell.imageView.image =part.thumbimage;
}
return cell;
}

和 detailViewdid 加载:

我没有收到任何错误或警告,什么都没有。就在我加载 6,7 个细节 View 时,motionManagers 似乎停止工作了......只是这个......我可以回到主视图并一次又一次地加载细节项目,但运动管理器不起作用。这是我的详细 View 的代码

@synthesize liveCounterLabel = _liveCounterLabel;
@synthesize resultLabel = _resultLabel;

@synthesize detailItem = _detailItem;
@synthesize imageView = _imageView;
@synthesize calculateButton = _calculateButton;
@synthesize motionManager =_motionManager;
@synthesize selectedImage=_selectedImage;
@synthesize unselectedImage=_unselectedImage;

@synthesize m11started=_m11started;
@synthesize m12started=_m12started;
@synthesize m13started=_m13started;
@synthesize m11ended=_m11ended;
@synthesize m12ended=_m12ended;
@synthesize m13ended=_m13ended;
@synthesize m11=_m11;
@synthesize m12=_m12;
@synthesize m13=_m13;
@synthesize queue=_queue;

@synthesize buttonCounter=_buttonCounter;



#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;

// Update the view.
[self configureView];

}
}

- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.imageView.image = [self.detailItem fullimageStartingPosition];
NSString* longString = [[self.detailItem bodypartname ] stringByAppendingString: [@" " stringByAppendingString:[self.detailItem movementname]]];
self.navigationItem.title = [NSString stringWithFormat:longString];
//Backround Image code
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]]];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Init motionManager object and set the Update Interval
_buttonCounter=0;


_motionManager = [[CMMotionManager alloc]init];
_motionManager.deviceMotionUpdateInterval=1/60; //60 Hz
[_motionManager startGyroUpdates];

if (!_queue){
_queue = [NSOperationQueue mainQueue];
}

if (_motionManager.gyroAvailable) {
_motionManager.gyroUpdateInterval = 1.0/60.0;
//[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
// withHandler: ^(CMDeviceMotion *motion, NSError *error)
[_motionManager startDeviceMotionUpdatesToQueue:_queue
withHandler: ^(CMDeviceMotion *motion, NSError *error)

{
CMAttitude *attitude = [[CMAttitude alloc] init];
attitude=motion.attitude;

//Calculation with rotationMatrix
_m11 = [NSString stringWithFormat:@"%.02f", attitude.rotationMatrix.m11];
_m12 = [NSString stringWithFormat:@"%.02f", attitude.rotationMatrix.m12];
_m13 = [NSString stringWithFormat:@"%.02f", attitude.rotationMatrix.m13];

}];

`

最佳答案

您应该检查 dequeueReusableCellWithIdentifier 的结果。如果它为 nil,则您可能在不属于您的内存部分中设置属性。

请引用这个doc (如果您想跳过章节,请搜索 dequeueReusableCellWithIdentifier)

关于ios - 内存管理 iOS 开发应用程序在一些细节项目后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10879550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com