0

我发现对于画廊中的大多数照片,[ALAsset thumbnail]将返回带有向内黑色半透明边框的缩略图。

我的问题是,我怎样才能获得没有这个边框的缩略图?

4

2 回答 2

0

没有 1 像素黑色边框的缩略图无法获得。

你也可以使用

 [asset aspectRatioThumbnail]; // but it is not rounded.

所以我认为你应该自己调整图像大小:

asset.defaultRepresentation.fullScreenImage or
asset.defaultRepresentation.fullResolutionImage
于 2013-05-04T11:28:06.857 回答
0

你有很多选择。如果您只需要在屏幕上显示它,您可以简单地伪造它,使缩略图的 1 个像素不可见。您可以将 UIImageView 放在剪辑到边界的 UIView 中。

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = [UIColor clearColor];
view.clipsToBounds = YES;
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(-1, -1, 202, 202)];
imgView.image = [asset thumbnail];
[view addSubview:imgView];

或者更好的是,创建一个 UIView 子类并覆盖 drawRect。

-(void)drawRect:(CGRect)rect
{
    UIImage* thumb = [asset thumbnail];
    [thumb drawInRect:CGRectMake(rect.origin.x-1, rect.origin.y-1, rect.size.width+2, rect.size.height+2)];
}

或者您可以改用 aspectRatioThumbnail 并自己使其平方。

UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
imgView.image = [asset aspectRatioThumbnail];
imgView.contentMode = UIViewContentModeScaleAspectFill;

或者,如果您出于某种原因实际上需要裁剪 UIImage 本身,您可以这样做。

UIImage* thumb = [asset thumbnail];
CGRect cropRect = CGRectMake(1, 1, thumb.size.width-2, thumb.size.height-2);
cropRect = CGRectMake(cropRect.origin.x*thumb.scale, cropRect.origin.y*thumb.scale, cropRect.size.height*cropRect.scale);       

CGImageRef imageRef = CGImageCreateWithImageInRect([thumb CGImage], cropRect);
UIImage* result = [UIImage imageWithCGImage:imageRef scale:thumb.scale orientation:thumb.imageOrientation]; 
CGImageRelease(imageRef);
于 2013-05-05T08:41:57.897 回答