Question

I have a UIscrollView I'm displaying an image in it in this way

- (UIImage *)pageControlImageWithIndex:(NSInteger)index{
NSString *imagePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/t%d.jpg",index];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:imagePath];
return [UIImage imageWithData:[fileHandle readDataToEndOfFile]];

}

UIImage *image = [self pageControlImageWithIndex:pageNumber];
UIGraphicsBeginImageContext(CGSizeMake(320.0, 164));
[image drawInRect:CGRectMake(0, 0, 320.0, 164.0)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];

but the image appear with some blur

enter image description here

while displaying the same image in this way in other View (inside UIImageView)

NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/t%d.jpg",topicNum]];
[topicPicImageView setImage:[UIImage imageWithData:[fileHandle readDataToEndOfFile]]];

produce this quality

enter image description here

HOW CAN I GET THE SAME QUALITY for the image in the UIScrollView ?

  • The image dimensions is 640 * 360
  • The Two image containers dimensions is 320 * 164
Was it helpful?

Solution

UIGraphicsBeginImageContext will create a context with a scale of 1.0, or non-retina. But you most likely want to create a context that matches the scale of your device. Use the new UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) function.

Replace

UIGraphicsBeginImageContext(CGSizeMake(320.0, 164));

with

UIGraphicsBeginImageContextWithOptions(CGSizeMake(320.0, 164), YES, 0.0);

0.0 as scale parameter means "use device main screen scale". If your image has transparency you want to use NO as second parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top