美文网首页tipsiOS技术点
iOS编程, 图片渐渐出现的效果

iOS编程, 图片渐渐出现的效果

作者: 霍伟健 | 来源:发表于2016-03-23 19:56 被阅读661次

    加载图片的时候通常会有渐渐出现的效果, 下面就是需要加的代码(SDWebImage第三方"sd_setImageWithURL: placeholderImage:" 中添加warning部分 代码):

    
    - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
        [self sd_cancelCurrentImageLoad];
        objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
        if (!(options & SDWebImageDelayPlaceholder)) {
            dispatch_main_async_safe(^{
                self.image = placeholder;
            });
        }
        
        if (url) {
    
            // check if activityView is enabled or not
            if ([self showActivityIndicatorView]) {
                [self addActivityIndicator];
            }
    
            __weak __typeof(self)wself = self;
            id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                [wself removeActivityIndicator];
                if (!wself) return;
                dispatch_main_sync_safe(^{
                    if (!wself) return;
                    if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock)
                    {
                        completedBlock(image, error, cacheType, url);
                        return;
                    }
                    else if (image) {
                        wself.image = image;
                        
                        
    
    
    
    
    
    
    
    
    #warning  图片渐渐出现的效果.
                        wself.alpha = 0;
                        if (cacheType != SDImageCacheTypeMemory) {
                            
                            [UIView animateWithDuration:1 animations:^{
                                wself.alpha = 1;
                            }];
                        }
                        else{
                            
                            wself.alpha = 1;
                        }
                        
                        
    
    
    
    
    
    
    
    
    
    
    
                        wself.alpha = 0;
                        
                        if (cacheType != SDImageCacheTypeMemory) {
                            
                            [UIView animateWithDuration:1 animations:^{
                                wself.alpha = 1;
                            }];
                        }
                        else{
                            
                            wself.alpha = 1;
                        }
                        
                        
                        [wself setNeedsLayout];
                    } else {
                        if ((options & SDWebImageDelayPlaceholder)) {
                            wself.image = placeholder;
                            [wself setNeedsLayout];
                        }
                    }
                    if (completedBlock && finished) {
                        completedBlock(image, error, cacheType, url);
                    }
                });
            }];
            [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
        } else {
            dispatch_main_async_safe(^{
                [self removeActivityIndicator];
                NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
                if (completedBlock) {
                    completedBlock(nil, error, SDImageCacheTypeNone, url);
                }
            });
        }
    }
    
    

    相关文章

      网友评论

        本文标题:iOS编程, 图片渐渐出现的效果

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