IOS:应用程序因内存警告而崩溃

时间:2013-04-01 04:45:23

标签: objective-c cocoa-touch crash automatic-ref-counting

当我在UITableView中加载带有缩略图的个人资料时,我的应用程序崩溃了。

最多可以加载5张配置文件并正确显示图片。但是,当我创建第六个,然后在表视图中加载配置文件时它会崩溃,向我显示内存警告消息。

其实我用ARC。所以,我无法释放内存。我只能使用autoreleasepool来重新释放内存。

以下是我用于在UITableView中添加个人资料图片的个人资料的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
{
     @autoreleasepool 
     {
         static NSString *CellIdentifier=@"Cell";
         UITableViewCell *cell = nil;

         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
         }

         MyProfile * objMP1 = [GlobalgroupedProfileArray objectAtIndex:[indexPath row]];
         NSString *titleText =[@"Name: " stringByAppendingString:objMP1.Name];
         cell.textLabel.text=titleText;

         NSString *strTitle1=[@"Owner: "stringByAppendingString:[objMP1.Owner  stringByAppendingString:@"\n"]];

         NSString *strTitle2=[@"Breed: "stringByAppendingString:[objMP1.Breed stringByAppendingString:@"\n"]];

         NSString *strTitle=[[strTitle1 stringByAppendingString:strTitle2] stringByAppendingString:[@"ID Number: "stringByAppendingString:[NSString stringWithFormat:@"%@",objMP1.IDNumber]]];

        cell.detailTextLabel.text=strTitle;
        cell.detailTextLabel.numberOfLines =3;

        @autoreleasepool 
        {
            UIImageView * imageView = [[UIImageView alloc]    initWithFrame:CGRectMake(cell.frame.size.width-70-25, 15, 50, 50)];
            imageView.image = objMP1.img;
            [cell addSubview:imageView];
            imageView=nil;
        }

        @autoreleasepool 
        {
            // Load the image with an GCD block executed in another thread

            dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader",  NULL);
            dispatch_async(downloadQueue, ^{

              CGSize newSize = CGSizeMake(50, 50);  //whaterver size
              UIGraphicsBeginImageContext(newSize);
              [objMP1.img drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
              UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext();

              dispatch_async(dispatch_get_main_queue(), ^{
                  @autoreleasepool 
                  {
                      UIImageView * imageView = [[UIImageView alloc]  initWithFrame:CGRectMake(cell.frame.size.width-70-25, 15, 50, 50)];
                      imageView.image = newImage;
                      [cell addSubview:imageView];
                      imageView=nil;
                  }
              });
          });
              dispatch_release(downloadQueue);
          }
       [cell.imageView setImage:newImage];
       cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
       return cell;
    }
}

任何提示?

0 个答案:

没有答案