2013-08-02 3 views
-2

여기 내 cellForRowAtIndexPath : 메서드를 사용하여 tableView에 데이터와 이미지를 채우는 방법을 설명합니다. 나는 크기가 같아 지도록 이미지 크기를 줄이려고합니다. 각 셀 행에 대해 두 개의 이미지가 있습니다. 하나는 모든 행에 대해 동일한 정적 이미지입니다. 이 이미지는 괜찮습니다. lazily 로딩되는 이미지 (cell.imageView.image)입니다. 특정 크기로 크기를 조정하려고합니다. 방금 UIImage의 높이와 너비를 통과해야 다음 코드UITableView 셀 이미지의 크기를 조절하는 방법

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:CellIdentifier]; 


    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    labelTitle.tag = 100; 
    labelTitle.textAlignment = UITextAlignmentLeft; 
    labelTitle.numberOfLines = 1; 
    labelTitle.font = [UIFont fontWithName:@"DIN-Bold" size:16]; 

    [cell.contentView addSubview:labelTitle]; 

    UILabel *labelDetail = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 320, 44)]; 
    labelDetail.tag = 101; 
    labelDetail.textAlignment = UITextAlignmentLeft; 
    labelDetail.numberOfLines = 1; 
    labelDetail.font = [UIFont fontWithName:@"DIN-Regular" size:14]; 

    [cell.contentView addSubview:labelDetail]; 

    // cell image 
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(250, 20, 36, 36)]; 
    imgView.image = [UIImage imageNamed:@"someImage.png"]; 
    imgView.contentMode = UIViewContentModeCenter; 
    imgView.alpha = 0.1; 
    [cell addSubview:imgView]; 

    CGSize itemSize = CGSizeMake(40, 40); 
    UIGraphicsBeginImageContext(itemSize); 
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); 
    [cell.imageView.image drawInRect:imageRect]; 
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


} 

NSUInteger row = indexPath.row; 

UILabel *labelTitle1 = (UILabel*)[cell viewWithTag:100]; 
labelTitle1.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; 


UILabel *labelDetail1 = (UILabel*)[cell viewWithTag:101]; 
labelDetail1.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"]; 


// set the image url & image caching 
    NSString *imageUrlString = [NSString stringWithFormat:@"%@", [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"]]; 
    UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString]; 

    if (cachedImage) 

    { 
     cell.imageView.image = cachedImage; 
    } 

    else 

    { 

     cell.imageView.image = [UIImage imageNamed:@"rest_small.png"]; // initialize the image with placeholder image 

     // download in the image in the background 
     [self.imageDownloadingQueue addOperationWithBlock:^{ 

      NSURL *imageUrl = [NSURL URLWithString:imageUrlString]; 
      NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; 
      UIImage *image = nil; 
      if (imageData) 
       image = [UIImage imageWithData:imageData]; 

      if (image) 
      { 

       [self.imageCache setObject:image forKey:imageUrlString]; // add the image to your cache 

       // finally, update the user interface in the main queue 

       [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
        // make sure the cell is still visible 

        UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath]; 
        if (updateCell) 
         cell.imageView.image = image; 

       }]; 
      } 
     }]; 
    } 


return cell; 

}

+0

이미지 크기를 조정하기 만하면 기존 질문을 간단히 확인해야합니다. 다음은이 사이트 자체에 대한 그런 질문의 링크입니다. http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uimimage – CodenameLambda1

+0

나는 몇 가지 다른 것을 찾았지만 아무 소용이 없었습니다. . 그래서 여기에 내 시간을 너무 많이 쓰는 것을 피하기 위해 코드를 게시했습니다. – hanumanDev

+0

검색 옵션에이 문자열 "ios image resize"를 입력하면 관련된 모든 게시물을 얻을 수 있습니다. – CodenameLambda1

답변

3

사용이 작물입니다/크기를 조정UIImage : 어떤 도움도 여기

에 대한

덕분에 내 코드입니다 너는 필요해.

이미지 자르기 얻기 위해 (당신은 이미지의 크기를 원하는대로 ) 여기

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect 
    { 
     //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15); 

     CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 
     UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
     CGImageRelease(imageRef); 

     return cropped; 
    } 

이 위로 돌아 Croped 이미지를 얻을 수 UIImage을 반환

UIImage *croppedImg = nil; 
CGRect cropRect = CGRectMake(AS YOu Need); 
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect]; 

를 사용하여 다음과 같은 방법을 방법;

또는

크기 조절 또한 크기 조정있는 UIImage에 대한 이미지으로 특정 높이와 폭 을 위해 다음과 같은 방법을 사용 :

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height 
{ 
    CGSize newSize = CGSizeMake(width, height); 
    float widthRatio = newSize.width/image.size.width; 
    float heightRatio = newSize.height/image.size.height; 

    if(widthRatio > heightRatio) 
    { 
     newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio); 
    } 
    else 
    { 
     newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio); 
    } 


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

이 메소드는 반환 NewImage, 원하는 특정 크기로.

관련 문제