天天看點

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];  
           

這樣就可以解決加載大圖會導緻的記憶體警告問題

繼續閱讀