使用缩放时,UIScrollView中的png或jpg质量松散

时间:2013-02-04 10:08:10

标签: iphone xcode

有关如何在ScrollView中使用放大时保持png或jpg质量的建议吗? 当我放大我的图像时,它们会全部失焦......

2 个答案:

答案 0 :(得分:1)

使用scrollViewDidEndZooming中的以下代码可能对您有所帮助:)

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
        CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
        CGImageRef imageRef = image.CGImage;

        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();

        // Set the quality level to use when rescaling
        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
        CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

        CGContextConcatCTM(context, flipVertical);  
        // Draw into the context; this scales the image
        CGContextDrawImage(context, newRect, imageRef);

        // Get the resized image from the context and a UIImage
        CGImageRef newImageRef = CGBitmapContextCreateImage(context);
        UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

        CGImageRelease(newImageRef);
        UIGraphicsEndImageContext();    

        return newImage;
    }

答案 1 :(得分:0)

您可以使用以下代码:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    CGSize nowSize = view.frame.size;
    if (nowSize.height >= self.myScrollView.frame.size.height)
        self.myScrollView.contentInset = UIEdgeInsetsZero;
    else {
        CGFloat delta = self.myScrollView.frame.size.height/2 - view.frame.size.height/2;
        self.myScrollView.contentInset = UIEdgeInsetsMake(delta, 0, delta, 0);
    }
}