iOS ASMediaFocusManager 缩略图预览

顾炎彬
2023-12-01

在做新浪微博客户端过程中,微博内容缩略图片放大显示的问题,在网上意外找到一个第三方库,很适合做这个工作,经过一点点研究,大概可以使用了。

    第三方库 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;
}

说明:代码比较简单,就不说了。

 类似资料: