在滑块裁剪后为什么UIImage模糊?

时间:2014-09-10 09:45:11

标签: ios objective-c uiimage cgimage cgimageref

我正在使用此代码为我的滑块裁剪UISlider的图像当我拖动滑块然后图像模糊它怎么能没有模糊和失去没有它的清晰度

- (UIImage *) revealedTrackImageForCurrentValues {
    //Get left and right x positions
    float lowerHandleWidth = _lowerHandleHidden ? 2.0f : _lowerHandle.frame.size.width;
    float upperHandleWidth = _upperHandleHidden ? 2.0f : _upperHandle.frame.size.width;
    float xLowerValue = ((self.bounds.size.width - lowerHandleWidth) * (_lowerValue - _minimumValue) / (_maximumValue - _minimumValue))+(lowerHandleWidth/2.0f);
    float xUpperValue = ((self.bounds.size.width - upperHandleWidth) * (_upperValue - _minimumValue) / (_maximumValue - _minimumValue))+(upperHandleWidth/2.0f);

    //Crop the image
    CGRect croppedImageRect = CGRectMake(xLowerValue, 0.0f, xUpperValue - xLowerValue, self.trackImage.size.height);
    CGImageRef croppedImageRef = CGImageCreateWithImageInRect([self.trackImage CGImage], croppedImageRect);
    UIImage *croppedImage = [UIImage imageWithCGImage:croppedImageRef];
    CGImageRelease(croppedImageRef);

    return croppedImage;

}

如何在不损失图像清晰度的情况下做到这一点?

1 个答案:

答案 0 :(得分:1)

编辑代码:

你必须继承UIImage:

的UIImageView + Cropping.h

#import <UIKit/UIKit.h>

@interface UIImage (Cropping)
- (UIImage *)revealedTrackImageForRect:(CGRect)croppedImageRect;
@end

的UIImageView + Cropping.m

#import "UIImage+Cropping.h"

@implementation UIImage (Cropping)


- (UIImage *)revealedTrackImageForRect:(CGRect)croppedImageRect
{
    //create drawing context
    UIGraphicsBeginImageContextWithOptions(croppedImageRect.size, NO, 0.0f);

    //draw
    [self drawAtPoint:CGPointMake(-croppedImageRect.origin.x, -croppedImageRect.origin.y)];

    //capture resultant image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return image
    return image;
}

@end

然后拨打您的密码:

#import "UIImage+Cropping.h"

- (UIImage *)revealedTrackImageForCurrentValues 
{
    //Get left and right x positions
    float lowerHandleWidth = _lowerHandleHidden ? 2.0f : _lowerHandle.frame.size.width;
    float upperHandleWidth = _upperHandleHidden ? 2.0f : _upperHandle.frame.size.width;
    float xLowerValue = ((self.bounds.size.width - lowerHandleWidth) * (_lowerValue - _minimumValue) / (_maximumValue - _minimumValue))+(lowerHandleWidth/2.0f);
    float xUpperValue = ((self.bounds.size.width - upperHandleWidth) * (_upperValue - _minimumValue) / (_maximumValue - _minimumValue))+(upperHandleWidth/2.0f);

    // Get rect
    CGRect croppedImageRect = CGRectMake(xLowerValue, 0.0f, xUpperValue - xLowerValue, self.trackImage.size.height);

    //Get cropped image
    UIImage *croppedImage = [self.trackImage revealedTrackImageForRect: croppedImageRect];

    return croppedImage;
}