【问题标题】:ALAsset thumbnail without inward black translucent border没有向内黑色半透明边框的 ALAsset 缩略图
【发布时间】:2013-05-04 11:21:17
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios objective-c alasset


    【解决方案1】:

    你有很多选择。如果您只需要在屏幕上显示它,您可以简单地伪造它,使缩略图的 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);
    

    【讨论】:

      【解决方案2】:

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

      你也可以使用

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

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

      asset.defaultRepresentation.fullScreenImage or
      asset.defaultRepresentation.fullResolutionImage
      

      【讨论】:

      • fullScreenImage 或 fullResolutionImage 会减慢代码速度,对吧?
      • 当然,如果图像大而不是明显变慢,那么最好使用缩略图
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多