2013-05-04 56 views

回答

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); 
0

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

您还可以使用

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

所以,我认为你应该使用自己调整图像:

asset.defaultRepresentation.fullScreenImage or 
asset.defaultRepresentation.fullResolutionImage 
+0

fullScreenImage或fullResolutionImage会减慢代码的速度,对吗? – 2013-05-04 11:34:21

+0

当然,如果图像比强烈减慢,那么更好的方法是使用缩略图 – 2013-05-04 11:42:04