Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义ImageView的内存溢出_Ios_Multithreading_Uiimageview_Out Of Memory_Nsobject - Fatal编程技术网

Ios 自定义ImageView的内存溢出

Ios 自定义ImageView的内存溢出,ios,multithreading,uiimageview,out-of-memory,nsobject,Ios,Multithreading,Uiimageview,Out Of Memory,Nsobject,我有一个对象,它是UIImageView的一个子类,用于检查另一个对象上的特定图像。如果有图像,ImageView将显示该图像,否则在下载新图像时将显示临时图像。下载完成后,ImageView将显示新图像。这个物体我称之为“成像仪” 我在tableview中的单元格上实例化了一个Imager。 由于某种原因,成像仪溢出了我应用程序的内存。在Imager实现之前,应用程序的内存使用量通常在60Mb左右,但在实现之后,它会达到300Mb,因此我收到内存警告,iPhone会关闭应用程序,正如预期的那样

我有一个对象,它是UIImageView的一个子类,用于检查另一个对象上的特定图像。如果有图像,ImageView将显示该图像,否则在下载新图像时将显示临时图像。下载完成后,ImageView将显示新图像。这个物体我称之为“成像仪”

我在tableview中的单元格上实例化了一个Imager。 由于某种原因,成像仪溢出了我应用程序的内存。在Imager实现之前,应用程序的内存使用量通常在60Mb左右,但在实现之后,它会达到300Mb,因此我收到内存警告,iPhone会关闭应用程序,正如预期的那样

我想知道,我做错了什么?这不是一个复杂的代码或逻辑,但我肯定在做一些提高内存的事情。我猜是桌子视图,但我不确定

以下是成像仪的代码: 成像仪

这是使用Imager对象的表

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"friendCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    Imager *accessory;
    friend *workingFriend = [thisUser.friendsList objectAtIndex:indexPath.row];
    cell.textLabel.text = workingFriend.username;
    //----------------Setup Accessory View-----------------------------------

                    accessory = [[Imager alloc]init];
                    [accessory loadFriendImage:[thisUser.friendsList objectAtIndex:indexPath.row]];
                    accessory.frame = CGRectMake(cell.accessoryView.frame.origin.x, cell.accessoryView.frame.origin.y, 72.0, 72.0);
                    accessory.contentMode = UIViewContentModeScaleAspectFill;
                    accessory.clipsToBounds = YES;



                    cell.accessoryView = accessory;

                    cell.textLabel.textColor = [UIColor blackColor];
                    [cell.textLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:17]];
                    //----------------------------------------------------------
  return cell;
}
这是下载图像的下载图像阻止功能

-(UIImage*)downloadImageBlocked
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://tatohouston.azurewebsites.net/getUserImg.php"]];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
    NSDictionary *tempDic;

    //---------------------------------
    tempDic = @{@"userid":userID};
    //serialize the dictionary data as json
    NSData *dataPost = [[tempDic copy] JSONValue];
    [request setHTTPBody:dataPost]; //set the data as the post body

    [request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)dataPost.length] forHTTPHeaderField:@"Content-Length"];

    NSData *tmpData = [[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] base64EncodedDataWithOptions:NSUTF8StringEncoding];
    if(tmpData != nil){
        returnData = [[NSData alloc]initWithBase64EncodedData:tmpData options:NSUTF8StringEncoding];
    }
    UIImage *friendImage;

    if(returnData != nil){
        friendImage = [UIImage imageWithData:returnData];
        userImg = friendImage;
        flagDownloaded = YES;
    }
    return friendImage;
}

从请求返回的NSData有多大?我看到它使用的是Base64编码的数据,一般来说比未编码的数据大1.37倍。我猜你的图像数据相当大

使用仪器检查由于内存保留而非泄漏而导致的泄漏和内存丢失。后者是仍然指向的未使用内存。在仪器上的分配仪器中使用标记生成(Heapshot)。有关如何使用Heapshot查找内存裂缝的信息,请参阅:次要建议:在创建变量时,friendInage应设置为nil,否则,如果returnData恰好为nil,此代码在返回其他随机指针时可能会导致错误。请求中的NSData约为121894字节,您认为可能是这样吗?通常加载这些数据的行数少于10行。请分析应用程序的内存分配情况,并准确确定占用所有内存的内容,然后按照上面@Zaph的建议进行报告。这将帮助你找到正确的方向。
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"friendCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    Imager *accessory;
    friend *workingFriend = [thisUser.friendsList objectAtIndex:indexPath.row];
    cell.textLabel.text = workingFriend.username;
    //----------------Setup Accessory View-----------------------------------

                    accessory = [[Imager alloc]init];
                    [accessory loadFriendImage:[thisUser.friendsList objectAtIndex:indexPath.row]];
                    accessory.frame = CGRectMake(cell.accessoryView.frame.origin.x, cell.accessoryView.frame.origin.y, 72.0, 72.0);
                    accessory.contentMode = UIViewContentModeScaleAspectFill;
                    accessory.clipsToBounds = YES;



                    cell.accessoryView = accessory;

                    cell.textLabel.textColor = [UIColor blackColor];
                    [cell.textLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:17]];
                    //----------------------------------------------------------
  return cell;
}
-(UIImage*)downloadImageBlocked
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://tatohouston.azurewebsites.net/getUserImg.php"]];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
    NSDictionary *tempDic;

    //---------------------------------
    tempDic = @{@"userid":userID};
    //serialize the dictionary data as json
    NSData *dataPost = [[tempDic copy] JSONValue];
    [request setHTTPBody:dataPost]; //set the data as the post body

    [request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)dataPost.length] forHTTPHeaderField:@"Content-Length"];

    NSData *tmpData = [[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] base64EncodedDataWithOptions:NSUTF8StringEncoding];
    if(tmpData != nil){
        returnData = [[NSData alloc]initWithBase64EncodedData:tmpData options:NSUTF8StringEncoding];
    }
    UIImage *friendImage;

    if(returnData != nil){
        friendImage = [UIImage imageWithData:returnData];
        userImg = friendImage;
        flagDownloaded = YES;
    }
    return friendImage;
}