user3684398
user3684398

Reputation: 99

Loss quality image in UIView

I've got a problem with my images on my app. In my slide-out menu, I've got an header where i put an image "header.png" which exist in two version "header.png" and "[email protected]". Here is my code how I implement it in my app:

- (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;
}

When I run the app on my phone (iPhone 4s), my image is pixelate, the edge are blowed and it's not really clean... I don't know where it comes from.

My images are 320x150px and 640x300px in 72dpi

Thx

--EDIT--

I've solved my problem by using an UIImageView see below:

- (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;
}

Upvotes: 1

Views: 95

Answers (1)

robert
robert

Reputation: 2842

The problem is that UIGraphicsBeginImageContextdoesn't provide you a retina image unless you use UIGraphicsBeginImageContextWithOptions(destinationSize, NO, scale)where scalecould be something like [UIScreen mainScreen].scale.

Just out of curiosity: why aren't you simply using

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

Upvotes: 1

Related Questions