iosobjective-cuiimagecgimagecgimageref

Why UIImage is Blurry after Cropping in Slider?


I am using this code for my slider for cropping image for UISlider when i am Dragging Slider then image is Blurring how can it without Blurring and Losing without its Clarity

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

}

how can i do it without lossing image clearity ??


Solution

  • Edited Code:

    You have to subclass 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
    

    And then call your code:

    #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;
    }