【问题标题】:Best pratice to draw clear image in UIScrollView在 UIScrollView 中绘制清晰图像的最佳实践
【发布时间】:2013-07-17 06:37:42
【问题描述】:

我有一个 UIscrollView 我正在以这种方式在其中显示图像

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

但图像看起来有些模糊

同时在其他View(UIImageView内部)以这种方式显示相同的图像

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

生产这种品质

如何在 UIScrollView 中获得相同质量的图像?

  • 图片尺寸为640 * 360
  • 两个图像容器尺寸为 320 * 164

【问题讨论】:

    标签: objective-c uiimageview uiimage uigraphicscontext


    【解决方案1】:

    UIGraphicsBeginImageContext 将创建一个比例为 1.0 或非视网膜的上下文。但是您很可能希望创建一个与您的设备规模相匹配的上下文。使用新的UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 函数。

    替换

    UIGraphicsBeginImageContext(CGSizeMake(320.0, 164));
    

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

    0.0 作为比例参数表示“使用设备主屏幕比例”。如果您的图像具有透明度,您希望使用 NO 作为第二个参数。

    【讨论】:

    • 真的很棒,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多