How use CGImageCreateWithImageInRect for iPhone 4 (HD)?

前端 未结 2 1120
不知归路
不知归路 2020-12-14 02:38

I use the following code to getting images from the sprite. And it works fine everywhere except the iPhone 4 (HD version).

- (UIImage *)croppedImage:(CGRect)         


        
2条回答
  •  Happy的楠姐
    2020-12-14 03:23

    You should multiply the crop rect by the image scale. From my experience, it's unnecessary to use any different image initilization.

    - (UIImage *)_cropImage:(UIImage *)image withRect:(CGRect)cropRect
    {
        cropRect = CGRectMake(cropRect.origin.x * image.scale,
                              cropRect.origin.y * image.scale,
                              cropRect.size.width * image.scale,
                              cropRect.size.height * image.scale);
    
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    
        UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    
        CGImageRelease(imageRef);
    
        return croppedImage;
    }
    

提交回复
热议问题