2014-06-20 74 views
1

我的應用程序中存在圖像問題。在我的滑出菜單中,我有一個標題,其中放置了一個圖像「header.png」,它存在於兩個版本「header.png」和「[email protected]」中。這裏是我的代碼如何實現它在我的應用程序:UIView中的損失質量圖像

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIImage *originalImage = [UIImage imageNamed:@"header.png"]; 
    CGSize destinationSize = CGSizeMake(320, 150); 
    UIGraphicsBeginImageContext(destinationSize); 
    [originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    UIImageView *headerView = [[UIImageView alloc] initWithImage:newImage]; 
    headerView.frame = CGRectMake(0,0, 320,150); 
    return headerView; 
} 

當我我的手機(iPhone 4S),我的形象是像素化上運行的應用程序,邊緣被炸飛,這不是真乾淨......我不知道它來自哪裏。

我的圖片都是72dpi的320x150px和640x300px

THX

- 編輯 -

我已經使用的UIImageView見下面的解決我的問題:

- (UIImageView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    NSString* fileName = [NSString stringWithFormat:@"header.png"]; 
    UIImage *newImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]]; 

    UIImageView *headerView = [[UIImageView alloc] initWithImage:newImage]; 
    headerView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 150); 

    return headerView; 
} 

回答

1

問題是,UIGraphicsBeginImageContext不會爲您提供視網膜圖像,除非您使用UIGraphicsBeginImageContextWithOptions(destinationSize, NO, scale)其中scale可能是一些像[UIScreen mainScreen].scale

只是出於好奇:你爲什麼不乾脆用

UIImage *newImage = [UIImage imageNamed:@"header.png"] 
return [[UIImageView alloc] initWithImage:newImage]; 
+0

感謝您的幫助,它更清晰。我對Objective-C完全陌生,所以我正在學習,我的代碼沒有真正優化。 – user3684398