Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从服务器调整图像大小并放置图像_Ios_Uiimage_Retina Display_Uigraphicscontext - Fatal编程技术网

Ios 从服务器调整图像大小并放置图像

Ios 从服务器调整图像大小并放置图像,ios,uiimage,retina-display,uigraphicscontext,Ios,Uiimage,Retina Display,Uigraphicscontext,我知道以前有人问过这个问题。。。但我认为我做的一切都很好,但是图像在视网膜显示器上看起来不太好。我听说如果需要在视网膜上显示UIImage,我所要做的就是将刻度设置为2.0。但仍然不起作用,我不知道图形的大小调整和裁剪是否会影响图像的质量。通常,我会从UIImageView的自定义类中的setImage调用我的大小调整器(来自服务器的图像它具有正确的宽度,但更高,因此假设它只进行裁剪),我会这样调用它: [customUIImageViewInstance setImage:[UIImage i

我知道以前有人问过这个问题。。。但我认为我做的一切都很好,但是图像在视网膜显示器上看起来不太好。我听说如果需要在视网膜上显示UIImage,我所要做的就是将刻度设置为2.0。但仍然不起作用,我不知道图形的大小调整和裁剪是否会影响图像的质量。通常,我会从UIImageView的自定义类中的setImage调用我的大小调整器(来自服务器的图像它具有正确的宽度,但更高,因此假设它只进行裁剪),我会这样调用它:

[customUIImageViewInstance setImage:[UIImage imageWithData:[impreso thumb] scale:isRetina()?2:1]];
在自定义UIImageView实现中:

-(void)setImage:(UIImage *)image{
    if (image==nil) {
        [super setImage:nil];
    }else{
        [super setImage:[self scaleAnImageAspectFillCropHeight:image withNewWide:self.frame.size.width andNewTall:self.frame.size.height]];
    }
}



-(UIImage*) scaleAnImageAspectFillCropHeight:(UIImage*) image withNewWide:(NSUInteger) wide andNewTall:(NSUInteger) tall{
    if (image.size.width!=wide || image.size.height!=tall){
        BOOL retina;
        if ([image scale]==2) {
            retina = YES;
            wide*=2;
            tall*=2;
        }else{
            retina = NO;
        }
        CGFloat width = image.size.width;
        CGFloat height = image.size.height;
        CGFloat targetWidth = wide;
        CGFloat targetHeight = tall;
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth;
        CGFloat scaledHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;

    scaleFactor = widthFactor; // the width must be all visible, the height might be cropped but that is the inteded behavior

    scaledWidth  = width * scaleFactor;
    scaledHeight = height * scaleFactor;

    // centers X if needed, Y starts in 0 because the top part of the image is important
    thumbnailPoint.y = 0;
    if (widthFactor < heightFactor){
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }


    UIGraphicsBeginImageContext(CGSizeMake(wide, tall)); // this will crop

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [image drawInRect:thumbnailRect];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    if (retina) {
        newImage = [UIImage imageWithCGImage:newImage.CGImage scale:2 orientation:newImage.imageOrientation];
    }

    if(newImage == nil){
        [self setContentMode:UIViewContentModeScaleAspectFit];
    }else{
        //pop the context to get back to the default
        UIGraphicsEndImageContext();
        return newImage;
    }
}
return image;
}
-(void)setImage:(UIImage*)image{
如果(图像==nil){
[super-setImage:nil];
}否则{
[super-setImage:[self-scaleAnimageAspectFillCropheght:图像具有newWide:self.frame.size.width和newtall:self.frame.size.height];
}
}
-(UIImage*)缩放图像AspectFillCropheight:(UIImage*)图像具有新宽:(NSInteger)宽和新球:(NSInteger)高{
if(image.size.width!=宽| | image.size.height!=高){
布尔视网膜;
如果([图像比例]==2){
视网膜=是;
宽*=2;
高*=2;
}否则{
视网膜=否;
}
CGFloat width=image.size.width;
CGFloat高度=image.size.height;
CGFloat targetWidth=宽;
CGFloat targetHeight=高;
CGFloat scaleFactor=0.0;
CGFloat标度宽度;
CGFloat标度高度;
CGPoint thumbnailPoint=CGPointMake(0.0,0.0);
CGFloat宽度系数=目标宽度/宽度;
CGFloat高度系数=目标高度/高度;
scaleFactor=widthFactor;//宽度必须全部可见,高度可能会被裁剪,但这是嵌入的行为
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
//居中X如果需要,Y从0开始,因为图像的顶部很重要
指钉点y=0;
if(宽度系数<高度系数){
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
UIGraphicsBeginImageContext(CGSizeMake(宽、高));//这将被裁剪
CGRect thumbnailRect=CGRectZero;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[image drawInRect:thumbnailRect];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
if(视网膜){
newImage=[UIImage imageWithCGImage:newImage.CGImage比例:2方向:newImage.imageOrientation];
}
if(newImage==nil){
[自设置内容模式:UIViewContentModeScaleSpectFit];
}否则{
//弹出上下文以返回默认值
UIGraphicsSendImageContext();
返回新图像;
}
}
返回图像;
}

我一直在调试。。。这个尺寸和比例是正确的。我不知道是什么影响了图像的质量。一个简单的解释会很有帮助,thx。

你是不是为了一个视网膜显示器而尝试将任何图像放大两倍?如果是,图像的质量不会变得更好。这是合理的。如您所知,图像的质量不是通过简单地拉伸宽度和高度来提高的


您真正需要做的是使用高分辨率图像,它最初的分辨率是原来的两倍

当然可以,但原始图像的宽度为宽*=2;甚至比高的还要高。。。实际上,非视网膜必须使其更小。不过还是谢谢你,我找到了我的虫子。它不是UIImageView的子类。。。insead是custum类的一个子类,它是UIImageView的子类,UIImageView也有setImage:override,所以它在该override中进行了其他类型的调整大小,这会产生奇怪的结果。。。但我给你+1,感谢你的帮助:)