iOS ASMediaFocusManager 縮圖預覽

來源:互聯網
上載者:User

在做新浪微部落格戶端過程中,微博內容縮圖片放大顯示的問題,在網上意外找到一個第三方庫,很適合做這個工作,經過一點點研究,大概可以使用了。

   
第三方庫 ASMediaFocusManager 可通過簡單的觸摸操作來放大映像並自動以動畫的方式填充全屏,再次觸摸映像或者點擊Done即可恢複原始大小。

是:https://github.com/autresphere/ASMediaFocusManager

下面通過官方文檔介紹這一一個用法(下載的檔案夾中包含了一個gif動畫示範,看了就知道怎麼回事了)。

ASMediaFocusManager

ASMediaFocusManager gives the ability to focus on any thumbnail image by a simple tap. The thumbnail image is automatically animated to a focused fullscreen image view. Another tap on the focused view shrinks the image back
to its initial position.

使用ASMediaFocusManager這個類庫可以通過單擊一個縮圖來放大映像,並以動畫方式填充全屏;再次單擊即可恢複原始縮圖。

Each thumbnail image view may have its own transform, the focus and defocus animations take care of any initial transform.Works on iPhone and iPad.

Orientation

The focused view is automatically adapted to the screen orientation even if your main view controller is portrait only.

視圖自動適應螢幕是方向。

Because orientation management is different between iOS 5 and 6, this class is for iOS 6 only (although it should not be hard to adapt it to iOS 5).

因為orientation management在iOS5和iOS6的處理是不同的,而這個類庫只支援iO6版本(儘管通過一定處理也不難實現支援iOS5)。

Use It

Copy the whole ASMediaFocusManager folder
in your project. 在你的項目中匯入這個類庫。

  • Create a ASMediaFocusManager(建立執行個體)
  • Implement its delegate (ASMediaFocusDelegate)
    (實現代理方法)
  • Declare all your views that you want to be focusable by calling [ASMediaFocusManager
    installOnViews:]  (調用方法)
Implementing

In your View Controller where some image views need focus feature, add this code.

- (void)viewDidLoad{    [super viewDidLoad];    self.mediaFocusManager = [[ASMediaFocusManager alloc] init];    self.mediaFocusManager.delegate = self;    // Tells which views need to be focusable. You can put your image views in an array and give it to the focus manager.    [self.mediaFocusManager installOnViews:self.imageViews];}

Here is an example of a delegate implementation. Please adapt the code to your context.

#pragma mark - ASMediaFocusDelegate// Returns an image that represents the media view. This image is used in the focusing animation view.// It is usually a small image.- (UIImage *)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager imageForView:(UIView *)view{    return ((UIImageView *)view).image;}// Returns the final focused frame for this media view. This frame is usually a full screen frame.- (CGRect)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager finalFrameforView:(UIView *)view{    return self.parentViewController.view.bounds;}// Returns the view controller in which the focus controller is going to be added.// This can be any view controller, full screen or not.- (UIViewController *)parentViewControllerForMediaFocusManager:(ASMediaFocusManager *)mediaFocusManager{    return self.parentViewController;}// Returns an URL where the image is stored. This URL is used to create an image at full screen. The URL may be local (file://) or distant (http://).- (NSURL *)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager mediaURLForView:(UIView *)view{    NSString *path;    NSString *name;    NSURL *url;    // Here, images are accessed through their name "1f.jpg", "2f.jpg", …    name = [NSString stringWithFormat:@"%df", ([self.imageViews indexOfObject:view] + 1)];    path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];    url = [NSURL fileURLWithPath:path];    return url;}// Returns the title for this media view. Return nil if you don't want any title to appear.- (NSString *)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager titleForView:(UIView *)view{    NSString *title;        title = [NSString stringWithFormat:@"Image %@", [self mediaFocusManager:mediaFocusManager mediaURLForView:view].lastPathComponent];        return @"Of course, you can zoom in and out on the image.";}
說明:前三個代理方法基本不用修改,第四個方法返回的是URL,如果本地已經存在縮圖的大圖,那麼就返回這個file的url(上面樣本給出了如何找到file的url的代碼);如果縮圖通過網路下載,那麼直接返回這個大圖下載的url。第五個方法是可以返回大圖的檔案名稱在顯示大圖時同步顯示,當然你也可以返回任何你想要在大圖顯示時同時顯示的字串。Configure(你還可以針對性的設定以下參數)

Here is the things you can configure:

  • focused background color
  • animation duration
  • enable/disable elastic animation
  • enable/disable zooming by pinch
  • close focused view either by tap or through a "Done" button (在舊版本中是使用tap方式,但是在新版本中使用的是“Done”button)
ARC

ASMediaFocusManager needs ARC.   (這個類庫使用ARC特性)

樣本實踐

下面通過自己寫一個簡單的demo進行嘗試,當然下載類庫的時候附帶了一個demo,做的是相當的好,看看那個demo大概就是什麼都懂了。

下面根據上面提到的步驟進行(使用單視圖模板)。

- (void)viewDidLoad{    [super viewDidLoad];        UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(35,10, 250, 143)];    iView.image = [UIImage imageNamed:@"1.jpg"];    [self.view addSubview:iView];        self.mediaFocusManager = [[ASMediaFocusManager alloc] init];    self.mediaFocusManager.delegate = self;    [self.mediaFocusManager installOnView:iView];}#pragma mark - ASMediaFocusDelegate- (UIImage *)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager imageForView:(UIView *)view{    return ((UIImageView *)view).image;}- (CGRect)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager finalFrameforView:(UIView *)view{    //    return self.parentViewController.view.bounds;    return self.view.bounds;}- (UIViewController *)parentViewControllerForMediaFocusManager:(ASMediaFocusManager *)mediaFocusManager{    //    return self.parentViewController;    return self;}- (NSURL *)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager mediaURLForView:(UIView *)view{    NSString *path;    NSString *name;    NSURL *url;        name = [NSString stringWithFormat:@"%df", 1];    path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];        url = [NSURL fileURLWithPath:path];    return url;}- (NSString *)mediaFocusManager:(ASMediaFocusManager *)mediaFocusManager titleForView:(UIView *)view;{    NSString *title;        title = [NSString stringWithFormat:@"Image %@", [self mediaFocusManager:mediaFocusManager mediaURLForView:view].lastPathComponent];        //    return @"Of course, you can zoom in and out on the image.";    return title;}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    [self.window setBackgroundColor:[UIColor whiteColor]];    [self.window makeKeyAndVisible];        ViewController *root = [[ViewController alloc]initWithNibName:nil bundle:nil];    self.window.rootViewController = root;    return YES;}

說明:代碼比較簡單,就不說了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.