【发布时间】:2013-12-06 20:57:58
【问题描述】:
我正在尝试在视频缩略图上添加视频播放器图标。
我从 YouTube API 获取图像,然后将其裁剪为正方形,然后将其调整为合适的大小。然后我在它上面添加我的播放器图标图像。
问题在于播放器图标比缩略图上的应该小得多(在屏幕上它是 28x28pt 时要小得多)。请参见下图,我将其添加到单元格中以显示其应为的大小,而不是缩略图大小:
我用这个方法把它裁剪成一个正方形:
/**
* Given a UIImage, return it with a square aspect ratio (via cropping, not smushing).
*/
- (UIImage *)createSquareVersionOfImage:(UIImage *)image {
CGFloat originalWidth = image.size.width;
CGFloat originalHeight = image.size.height;
float smallestDimension = fminf(originalWidth, originalHeight);
// Determine the offset needed to crop the center of the image out.
CGFloat xOffsetToBeCentered = (originalWidth - smallestDimension) / 2;
CGFloat yOffsetToBeCentered = (originalHeight - smallestDimension) / 2;
// Create the square, making sure the position and dimensions are set appropriately for retina displays.
CGRect square = CGRectMake(xOffsetToBeCentered * image.scale, yOffsetToBeCentered * image.scale, smallestDimension * image.scale, smallestDimension *image.scale);
CGImageRef squareImageRef = CGImageCreateWithImageInRect([image CGImage], square);
UIImage *squareImage = [UIImage imageWithCGImage:squareImageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(squareImageRef);
return squareImage;
}
用这个方法调整它的大小:
/**
* Resize the given UIImage to a new size and return the newly resized image.
*/
- (UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
并使用此方法将其添加到其他图像之上:
/**
* Adds a UIImage on top of another UIImage and returns the result. The top image is centered.
*/
- (UIImage *)addImage:(UIImage *)additionalImage toImage:(UIImage *)backgroundImage {
UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[additionalImage drawInRect:CGRectMake((backgroundImage.size.width - additionalImage.size.width) / 2, (backgroundImage.size.height - additionalImage.size.height) / 2, additionalImage.size.width, additionalImage.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
这就是它的实现方式:
UIImage *squareThumbnail = [self resizeImage:[self createSquareVersionOfImage:responseObject] toSize:CGSizeMake(110.0, 110.0)];
UIImage *playerIcon = [UIImage imageNamed:@"video-thumbnail-overlay"];
UIImage *squareThumbnailWithPlayerIcon = [self addImage:playerIcon toImage:squareThumbnail];
但最后,图标总是太小。在处理图像时,尺寸问题让我感到困惑,因为我习惯于自动找出与视网膜屏幕相关的东西,例如在上面的代码块中,我不确定为什么我将它设置为 110.0, 110.0,因为它是55x55 UIImageView,我认为它会自动缩放(但如果我把它设置为 55,它会被拉伸得很厉害)。
【问题讨论】:
-
这不能回答你的问题,但我建议使用 UIViews 合成图像,而不是从头开始烘焙 UIImage。虽然您的方法不会对少量图像造成太大问题,但当您想要显示 20 或 100 个图像时,您可能会发现性能(尤其是滚动)会受到影响。
-
我不能做 UIViews 因为它需要被保存到核心数据。但我想我可以单独保存它们。那么构建两个图像的 UIView 在性能上会更好吗?这同样适用于将第二个 UIImage 添加为第一个 UIImageView 的子视图吗?
-
我不知道你的情况的所有细节。话虽如此,在实现了非常相似的东西之后,我发现尝试对图像进行大量预处理可能会变得非常棘手,尤其是当您决定更改图形或设计时。您列出的操作都可以通过核心图形和 UIKit 非常流畅地处理。我做过的唯一预处理是确保缩小尺寸的超大图像。 (例如,当我尝试显示它时,我得到了一张 4096x4096 的图像,它在旧的 ipad 上显示它。) UIImageViews 会很容易地处理你想要做的事情。
标签: ios objective-c uiimageview uiimage core-graphics