我发现对于画廊中的大多数照片,[ALAsset thumbnail]
将返回带有内侧黑色半透明边框的缩略图。
我的问题是,如何才能在没有这个边框的情况下获得缩略图?
发布于 2013-05-05 16:41:58
你有很多选择。如果你只需要在屏幕上显示它,你可以简单地伪造它,这样缩略图的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-04 19:28:07
没有方法可以在没有1像素黑色边框的情况下获得缩略图。
您还可以使用
[asset aspectRatioThumbnail]; // but it is not rounded.
所以我认为你应该自己调整图像的大小:
asset.defaultRepresentation.fullScreenImage or
asset.defaultRepresentation.fullResolutionImage
https://stackoverflow.com/questions/16373710
复制相似问题