Search code examples
iosobjective-cimagecocoa-touchnsdata

Alternative to NSData in image loading


I currently have a RSS feed loading into a Navigation Controller, but I just recently added images onto the cells, I am using NSData to store the image data, but it is very laggy so this means I need a better way to store that data.

This is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    NSString *textFiltered = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
    cell.detailTextLabel.text = [textFiltered stringByConvertingHTMLToPlainText];
    //cell.detailTextLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"];
    //NSString *imageURLString = [[feeds objectAtIndex:indexPath.row] objectForKey: @"enclosure"];

    NSURL *url = feeds[indexPath.row][@"url"];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    cell.imageView.image = [UIImage imageWithData:imageData];


    return cell;
}

Any good alternatives and if so how do I use it correctly?


Solution

  •     NSURL *url=[NSURL URLWithString:urlUTF8];
    
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    
        [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
         {
             UIImage *imagemain=[UIImage imageWithData:data];
             //         CGSize size=imagemain.size;
      //compulsary you have to give resize the image.....
             UIImage *compimage=[self  resizeImage:imagemain resizeSize:CGSizeMake(45,45)];
             Cell.imgProfile.image=compimage;
             //         CGSize size1=compimage.size;
         }];
     this is perfect code for image loading in tableview cell.......
    
    for Resizing Method
    
    -(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size
     {
    CGFloat actualHeight = orginalImage.size.height;
    CGFloat actualWidth = orginalImage.size.width;
    //  if(actualWidth <= size.width && actualHeight<=size.height)
    //  {
    //      return orginalImage;
    //  }
    float oldRatio = actualWidth/actualHeight;
    float newRatio = size.width/size.height;
    if(oldRatio < newRatio)
    {
        oldRatio = size.height/actualHeight;
        actualWidth = oldRatio * actualWidth;
        actualHeight = size.height;
    }
    else
    {
        oldRatio = size.width/actualWidth;
        actualHeight = oldRatio * actualHeight;
        actualWidth = size.width;
    }
    
    CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [orginalImage drawInRect:rect];
    orginalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return orginalImage;
    

    }