天天看点

MWPhotoBrowser的基本使用及图片尺寸的更改

网络上现在有好多图片显示、缓存、下载的第三方,实现的原理大都是通过SDWebImage进行图片处理,简单记录一下我对MWPhoto一些用法的理解,git的下载地址

其实一般的用法在git上写的很详细了

下载,导入到工程,实现代理方法,配置一些信息

self.photos = [NSMutableArray array];//创建一个数组,实现MWPhotoBrowser代理方法时用到

    [photos addObject:[MWPhoto photoWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]]]];//导入本地图片
    [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b.jpg"]]];//导入网络图片

    MWPhotoBrowser *_browser = [[MWPhotoBrowser alloc] initWithDelegate:self];

    _browser.displayActionButton = YES;//是否有分享按钮
    _browser.displayNavArrows = YES;//底部菜单是否存在左右点击
    _browser.displaySelectionButtons = YES;//是否显示选择按钮在图片上
    _browser.alwaysShowControls = NO;//单击菜单是否不可隐藏
    _browser.zoomPhotosToFill = NO;//是否全屏显示
    _browser.enableGrid = YES;//是否显示底部菜单
    _browser.startOnGrid = NO;//进入是否时显示全部照片
    _browser.autoPlayOnAppear = NO;

    [_browser setCurrentPhotoIndex:];//初始进入显示第几张照片
           

然后实现代理方法,类似于tableView,设置图片总数,显示等

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}
           

在这里显示出来图片后,可以根据自己的需要,在MWZoomingScrollView中更改手势的方法,并且可以去修改双击方法图片的尺寸等,在setMaxMinZoomScalesForCurrentBounds这个方法中可以找到所需要修改图片放大尺寸的相关信息;

如果你所加载的网络图片需要请求头文件,你可以手动的去给SDWebImage加header,在MWPhoto.m中的_performLoadUnderlyingImageAndNotifyWithWebURL方法里,改写SDWebImageManager

SDWebImageDownloader *manager11 = [SDWebImageManager sharedManager].imageDownloader;
       for (NSString *headerKey in [newHeaders allKeys])
            {
                [manager11 setValue:[newHeaders objectForKey:headerKey] forHTTPHeaderField:headerKey];
            }
        _webImageOperation = [manager11 downloadImageWithURL:url options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            if (expectedSize > ) {
                float progress = receivedSize / (float)expectedSize;
                NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithFloat:progress], @"progress",
                                      self, @"photo", nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_PROGRESS_NOTIFICATION object:dict];
            }
        } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            if (error) {
                MWLog(@"SDWebImage failed to download image: %@", error);
            }
            _webImageOperation = nil;
            if (image) {
                self.underlyingImage = image;
            }else{
                self.underlyingImage = [UIImage imageWithData:data];
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                if (strongSelf) {
                    [strongSelf imageLoadingComplete];//已修改,看
                }
            });
        }];
           

有时MWPhotoBrowser中的SDWebImage加载大图会导致的内存警告问题,SDWebImage有一个SDWebImageDownloaderOperation类来执行下载操作的。其中connectionDidFinishLoading方法的原理是将data转换成image。

然后在UIImage+MultiFormat里面有一个方法sd_imageWithData:,是UIImage的一个类别处理。image = [[UIImage alloc] initWithData:data]; SDWebImage把下载下来的data直接转成image,然后没做等比缩放直接存起来使用。所以需要在这边做处理:

UIImage+MultiFormat添加一个方法:

+(UIImage *)compressImageWith:(UIImage *)image  
     {  
        float imageWidth = image.size.width;  
       float imageHeight = image.size.height;  
        float width = ;  
        float height = image.size.height/(image.size.width/width);  

        float widthScale = imageWidth /width;  
        float heightScale = imageHeight /height;  

        // 创建一个bitmap的context  
       // 并把它设置成为当前正在使用的context  
       UIGraphicsBeginImageContext(CGSizeMake(width, height));  

       if (widthScale > heightScale) {  
           [image drawInRect:CGRectMake(, , imageWidth /heightScale , height)];  
.       }  
        else {  
            [image drawInRect:CGRectMake(, , width , imageHeight /widthScale)];  
        }  

        // 从当前context中创建一个改变大小后的图片  
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
        // 使当前的context出堆栈  
        UIGraphicsEndImageContext();  

        return newImage;  

    }
           

然后在:image = [[UIImage alloc] initWithData:data];下面调用以下:

if (data.length/1024 > 1024) {  
                image = [self compressImageWith:image];  
            } 
           

最后在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的

UIImage *image = [UIImage sd_imageWithData:self.imageData];  

     //将等比压缩过的image再赋给转成data赋给self.imageData  
     NSData *data = UIImageJPEGRepresentation(image, 1);  
     self.imageData =  [NSMutableData dataWithData:data];  
           

这样就可以解决加载大图会导致的内存警告问题

继续阅读