2012-12-04 3 views
0

오른쪽 셀에 표시되도록 웹 서버에서 이미지 세트를로드하고 있습니다. 그러나 이미지는 다른 크기로 제공되므로 목록이 표시 될 때라도 유엔 상태로 보입니다. 이미지를 100 x 80과 같은 고정 크기로 설정할 수 있습니까?xcode ios : 셀 이미지 크기를 설정하십시오.

cell.lotImageView.image = [UIImage imageNamed:@"blankthumbnail.png"]; 
cell.lotImageView.clipsToBounds = YES; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 
    //load image from web server 
    NSString *strURL = [NSString stringWithFormat:@"%@/images/%@", user.url, lotPhoto[row]]; 
    NSURL *url = [[NSURL alloc] initWithString:strURL ]; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     // let's make sure the cell is still visible (i.e. hasn't scrolled off the screen) 
     mainTableCell *cell = (mainTableCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     if (cell) 
     { 
      cell.lotImageView.clipsToBounds = YES; 
      cell.lotImageView.image =image; 
     } 
    }); 
}); 
+1

예상 크기로 이미지 크기를 조정하는 최고의 자습서 : http://mobiledevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html –

+0

imageView에 프레임 추가 cell.lotImageView setFrame : CGrectMake (0,0,100,80)]; – AppleDelegate

답변

0

당신은 당신의 이미지 크기를 조정하기 위해 시도 할 수 있습니다 :이 섹션 따를

내 코드입니다. 이 코드는 다음과 같이 만들었습니다.

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return destImage; 
} 
+0

당신은 .m 파일에이 코드를 넣는 것을 의미합니까? 전화 할 필요가 없습니까? – dan

+0

당신이 당신의 경우에 호출해야하는 메소드입니다. .m에서이 메소드를 추가하고 라인을 변경하십시오. UIImage * image = [UIImage imageWithData : [NSData dataWithContentsOfURL : url]]; UIImage * image = [self imageWithImage : [UIImage imageWithData : [NSData dataWithContentsOfURL : url]] convertToSize : CGSizeMake (100, 80)]; – Illuyankas

+0

그것이 작동하지 않으면 코드를 사용하고 afterline을 추가하십시오 cell.lotImageView.image = image; 이 코드 cell.lotImageView setFrame : CGrectMake (cell.lotImageView.frame.origin.x, cell.lotImageView.frame.origin.y, 100,80)]; 일반적으로 이것으로 UIImageView와 UIImage의 크기가 적당합니다. – Illuyankas

1

다음은 원하는 이미지 크기를 얻을 수있는 방법입니다. 아래 기능을 응용 프로그램 위임에 넣고 응용 프로그램 전체에서 사용할 수 있습니다. 여기에 코드입니다 :

+(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; 
     //NSLog(@"hi thassoods"); 
    } 
    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; 
} 

그러나 당신에 의해 지정된 새로운 크기가 이미지의 원래 크기에 비례하지 않으면 그래, 그때는 이미지 해상도가 제대로 유지되지 않을 수 수 있습니다.

모두 최고 !!!

관련 문제