如何在 ios 中使用 "css sprites"?

How can I use "css sprites" in ios?

我有一个包含多个图标的图像,并且我有我想要显示的图标的位置和大小。

问题是,我怎样才能在 UIImageView 中只显示图像的一部分,以便只显示我想要显示的图标?

是否可以在 1x、2x 和 3x 中正确显示图标,即使图像有点像素化?

您可以裁剪图像的一部分并使用 CGImageCreateWithImageInRect 从中创建新的 UIImage:

CGRect cropRect = CGRectMake(x,y,width,height); //Calculate the rect you'd like to show
CGImageRef imageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cropRect);
UIImage* outImage = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation]; 
CGImageRelease(imageRef);