美文网首页悦览树
iOS 图片显示优化

iOS 图片显示优化

作者: 南城同學 | 来源:发表于2017-08-05 09:10 被阅读0次

    将:

    self.imageView.image = [UIImage imageNamed:@"image_01"];
    

    替换为:

    - (void)image {
    
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(100, 100, 100, 56);
        [self.view addSubview:imageView];
        self.imageView = imageView;
    
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            // 获取CGImage
            CGImageRef cgImage = [UIImage imageNamed:@"image_01"].CGImage;
    
            // alphaInfo
            CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(cgImage) & kCGBitmapAlphaInfoMask;
            BOOL hasAlpha = NO;
            if (alphaInfo == kCGImageAlphaPremultipliedLast ||
                alphaInfo == kCGImageAlphaPremultipliedFirst ||
                alphaInfo == kCGImageAlphaLast ||
                alphaInfo == kCGImageAlphaFirst) {
                hasAlpha = YES;
            }
    
            // bitmapInfo
            CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host;
            bitmapInfo |= hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst;
    
            // size
            size_t width = CGImageGetWidth(cgImage);
            size_t height = CGImageGetHeight(cgImage);
    
            // context
            CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0, CGColorSpaceCreateDeviceRGB(), bitmapInfo);
    
            // draw
            CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
    
            // get CGImage
            cgImage = CGBitmapContextCreateImage(context);
    
            // into UIImage
            UIImage *newImage = [UIImage imageWithCGImage:cgImage];
    
            // release
            CGContextRelease(context);
            CGImageRelease(cgImage);
    
            // back to the main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imageView.image = newImage;
            });
        });
    }
    

    相关文章

      网友评论

        本文标题:iOS 图片显示优化

        本文链接:https://www.haomeiwen.com/subject/iodilxtx.html