imageOptClient 1.0.0

imageOptClient 1.0.0

Bhavesh BHOJANI 维护。



imageOptClient - Swift

imageOptClient 是一个 iOS 的 imageOpt 客户端库,用于从在 Image CDN 上创建的普通 imageSet URL 构建参数化图像 Opt URL

更多详细信息请参阅 Image CDN

CocoaPods 安装

CocoaPods 是 Objective-C 和 Swift 的依赖管理器,它自动简化并简化了在项目中使用第三方库的过程。有关更多信息,请参阅 入门 部分。

Podfile

pod 'imageOptClient'

文档

构建带有imageView的URL

func constructURL(imageURL:String, imageView:UIImageView, crop:Bool,
                  completionHandler: @escaping (_ imageOptURL: URL?) -> Void)

该函数用于构建一个包含查询参数的图像Opt URL,给定在图片CDN创建的图像集URL和imageView。此函数等待imageView大小确定,然后使用给定的图像集URL、imageView大小和裁剪偏好构建参数化的imageOpt URL,最后以该URL调用completionHandler。

当在调用时未知或未最终确定大小时,请使用此函数。

/* Parameters:
 *   imageURL : specifies the url of the imageSet to be loaded
 *   imageView : specifies the image view into which the image will loaded/displayed
 *   crop : whether or not, the image can be cropped if needed to match the requested size
 *   completionHandler: completion block will be called with parameterized imageOpt url
 */

基于当前系统区域/语言构建带有imageView的URL

func constructURL(imageURL:String, imageView:UIImageView, 
                  crop:Bool, useSystemLocale:Bool,
                  completionHandler: @escaping (_ imageOptURL: URL?) -> Void)

该函数用于构建一个包含查询参数的图像Opt URL,给定在图片CDN创建的图像集URL和imageView。此函数等待imageView大小确定,然后使用给定的图像集URL、imageView大小、裁剪偏好和当前设备区域/语言构建参数化的imageOpt URL,最后以该URL调用completionHandler。

当在调用时未知或未最终确定大小时,请使用此函数。

/* Parameters:
 *   imageURL : specifies the url of the imageSet to be loaded
 *   imageView : specifies the image view into which the image will loaded/displayed
 *   crop : whether or not, the image can be cropped if needed to match the requested size
 *   useSystemLocale: specifies that system locale/language to be used
 *   completionHandler: completion block will be called with parameterized imageOpt url
 */

基于显式区域参数构建带有imageView的URL

func constructURL(imageURL:String, imageView:UIImageView, 
                  crop:Bool, locale:Locale,
                  completionHandler: @escaping (_ imageOptURL: URL?) -> Void)

该函数用于构建一个包含查询参数的图像Opt URL,给定在图片CDN创建的图像集URL、imageView和区域。此函数等待imageView大小确定,然后使用给定的图像集URL、imageView大小、裁剪偏好和区域偏好构建参数化的imageOpt URL,最后以该URL调用completionHandler。

当在调用时未知或未最终确定大小时,请使用此函数。

/* Parameters:
 *   imageURL : specifies the url of the imageSet to be loaded
 *   imageView : specifies the image view into which the image will loaded/displayed
 *   crop : whether or not, the image can be cropped if needed to match the requested size
 *   locale : specifies the preferred language of the image
 *   completionHandler: completion block will be called with parameterized imageOpt url
 */

基于imageSize构建URL

func constructURL(imageURL:String, imageSize:CGSize, crop:Bool ) -> URL?

该函数用于构建一个包含查询参数的图像Opt URL,给定在图片CDN创建的图像集URL和imageSize。此函数使用给定的图像集URL、imageSize和裁剪偏好构建并返回参数化的imageOpt URL。

当在调用时已知或已最终确定大小时,请使用此函数。

/* Parameters:
 *   imageURL : specifies the url of the imageSet to be loaded
 *   imageSize : specifies the image size to be loaded/displayed
 *   crop : whether or not, the image can be cropped if needed to match the requested size
 *
 * Returns:
 *   imageOptURL : imageOpt url with query parameters, it can be used to fetch the required image
 */

使用图像大小和当前系统区域语言构造URL

func constructURL(imageURL:String, imageSize:CGSize, 
                       crop:Bool, useSystemLocale:Bool) -> URL?

给定在Image CDN创建的图像集URL和图像大小,构造带有查询参数的imageOpt URL的函数。此函数使用给定的图像集URL、图像大小、裁剪偏好和设备的当前区域/语言构造并返回参数化的imageOpt URL。

当在调用时已知或已最终确定大小时,请使用此函数。

/* Parameters:
 *   imageURL : specifies the url of the imageSet to be loaded
 *   imageSize : specifies the image size to be loaded/displayed
 *   crop : whether or not, the image can be cropped if needed to match the requested size
 *   useSystemLocale: specifies that system locale/language to be used
 *
 * Returns:
 *   imageOptURL : imageOpt url with query parameters, it can be used to fetch the required image
 */

使用图像大小和显式区域参数构造URL

func constructURL(imageURL:String, imageSize:CGSize, 
                       crop:Bool, locale:Locale) -> URL?

给定在Image CDN创建的图像集URL、图像大小和区域,构造带有查询参数的imageOpt URL的函数。此函数使用给定的图像集URL、图像大小、裁剪偏好和区域偏好构造并返回参数化的imageOpt URL。

当在调用时已知或已最终确定大小时,请使用此函数。

/* Parameters:
 *   imageURL : specifies the url of the imageSet to be loaded
 *   imageSize : specifies the image size to be loaded/displayed
 *   crop : whether or not, the image can be cropped if needed to match the requested size
 *   locale : specifies the preferred language of the image
 *
 * Returns:
 *   imageOptURL : imageOpt url with query parameters, it can be used to fetch the required image
 */

Usage - Swift

首先添加导入语句

import imageOptClient

A. 假设你想要在自定义UITableViewCell named CustomTableViewCell中加载图像,该自定义cell有一个IBOutlet named customImageView,其最终大小只有在其已经布局之后才会知道。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath as IndexPath) as! CustomTableViewCell

    //Assuming the presence of array named imageList with cell data about each image item
    if let cellData = imageList[indexPath.row] {

        ...

        //Assuming cellData has a parameter named imageURL which has the url of the image to be loaded in the cell
        if let imageURL = cellData["imageURL"] as? String {

            //Construct a imageOpt URL using the customImageView, since this is a table view we will prefer to crop the image
            imageOptClient.constructURL(imageURL: imageURL, imageView: cell.customImageView,
                                          crop: true, completionHandler: { imageOptURL in

    	        // Here we use SDWebImage to fetch, you can use any method of your choice,
    	        // just make sure standard cache control is enabled
                cell.customImageView.sd_setImage(with: imageOptURL, placeholderImage:UIImage(named: "placeholder"),
                                              options: .refreshCached, completed: nil)
            })
        }
    }
    return cell
}

B. 假设你有一个普通的ViewController(不是Collection/Table ViewController),你也还有一个IBOutlet imageView,并且想要将图像加载到其中

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // Use the URL of imageSet obtained from imageOpt.com, this can be directly used or fetched from backend server
    let imageURLString = "https://p1.imageopt.net/9zt-ct/q"

    // Get the imageOpt url with query parameters to be used to fetch the image
    // Lets say this is a details type of view, so we will prefer not to crop the image, hence set crop to false
    let imageOptURL = imageOptClient.constructURL(imageURL: imageURLString, imageSize: imageView.frame.size, crop: false)

    // Here we use SDWebImage to fetch, you can use any method of your choice,
    // just make sure standard cache control is enabled
    imageView.sd_setImage(with: imageOptURL, placeholderImage:UIImage(named: "placeholder"),
                       options: .refreshCached, completed: nil)
}        

C. 假设你想要在UITableViewCell中加载图像,图像大小是固定的,我们想要加载与系统语言对应的图像。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath as IndexPath) as! CustomTableViewCell

    //Assuming the presence of array named imageList with cell data about each image item
    if let cellData = imageList[indexPath.row] {

        ...

        //Assuming cellData has a parameter named imageURL which has the url of the image to be loaded in the cell
        if let imageURL = cellData["imageURL"] as? String {

            //Construct a imageOpt URL using the customImageView, since this is a table view we will prefer to crop the image
            let imageOptURL = imageOptClient.constructURL(imageURL: imageURLString, 
                         imageSize: CGSize(width: 200,height: 200), crop: true,
                         useSystemLocale: true)

            // Here we use SDWebImage to fetch, you can use any method of your choice,
            // just make sure standard cache control is enabled
            cell.imageView.sd_setImage(with: imageOptURL, placeholderImage:UIImage(named: "placeholder"),
                         options: .refreshCached, completed: nil)
        }
    }
    return cell
}

D. 假设你有一个普通的ViewController(不是Collection/Table ViewController),你也还有一个IBOutlet imageView,并且想要从viewDidLoad方法中加载特定语言的图像

override func viewDidLoad() {
    super.viewDidLoad()

    // Use the URL of imageSet obtained from imageOpt.com, this can be directly used or fetched from backend server
    let imageURLString = "https://p1.imageopt.net/9zt-ct/q"

    // Create an instance of Locale and intialize it to say french language
    let locale = Locale(identifier: "fr")

    // Get the imageOpt url with query parameters to be used to fetch the image
    // Lets say this is a details type of view, so we will prefer not to crop the image, hence set crop to false
    imageOptClient.constructURL(imageURL: imageURL, imageView: self.imageView,
                                crop: false, locale: locale,
                                completionHandler: { imageOptURL in

    	 // Here we use SDWebImage to fetch, you can use any method of your choice,
    	 // just make sure standard cache control is enabled
         self.imageView.sd_setImage(with: imageOptURL, placeholderImage:UIImage(named: "placeholder"),
                                              options: .refreshCached, completed: nil)
    })
}

Usage - ObjectiveC

首先添加导入语句

#import <imageOptClient-Swift.h>

A. 假设你想要在自定义UITableViewCell named CustomTableViewCell中加载图像,该自定义cell有一个IBOutlet named customImageView,其最终大小只有在其已经布局之后才会知道。

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *tableIdentifier = @"MyTableCell";
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = (CustomTableViewCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }

    //Assuming the presence of an array named imageURLList with imageOpt URL for each item
    NSString* imageURL = imageURLList[indexPath.row];

    //Construct a imageOpt URL using the customImageView, since this is a table view we will prefer to crop the image
    [imageOptClient constructURLWithImageURL:imageURL imageView:cell.customImageView crop:true 
        completionHandler: ^(NSURL* imageOptURL) {
    	        // Here we use SDWebImage to fetch, you can use any method of your choice,
    	        // just make sure standard cache control is enabled
              [cell.customImageView sd_setImageWithURL:imageOptURL
                                    placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
                                    options:SDWebImageRefreshCached completed: nil];
    }];
    
    return cell;
}

B. 假设你有一个普通的ViewController(不是Collection/Table ViewController),你也还有一个IBOutlet imageView,并且想要将图像加载到其中

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // Use the URL of imageSet obtained from imageOpt.com, this can be directly used or fetched from backend server
    NSString *imageURLString = @"https://p1.imageopt.net/9zt-ct/q";

    // Get the imageOpt url with query parameters to be used to fetch the image
    // Lets say this is a details type of view, so we will prefer not to crop the image, hence set crop to false
    NSURL *imageOptURL = [imageOptClient constructURLWithImageURL:imageURLString 
                                                        imageSize:self.imageView.frame.size crop:false];

    // Here we use SDWebImage to fetch, you can use any method of your choice,
    // just make sure standard cache control is enabled
    [self.imageView sd_setImageWithURL:imageOptURL 
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
                      options:SDWebImageRefreshCached completed: nil];

}

C. 假设你想要在UITableViewCell中加载图像,图像大小是固定的,我们想要加载与系统语言对应的图像。

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *tableIdentifier = @"MyTableCell";
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = (CustomTableViewCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }

    //Assuming the presence of an array named imageURLList with imageOpt URL for each item
    NSString* imageURL = imageURLList[indexPath.row];

    //Construct a imageOpt URL using fixed size 100x100, since this is a table view we will prefer to crop the image
    NSURL *imageOptURL = [imageOptClient constructURLWithImageURL:imageURLString 
                             imageSize:CGSizeMake(100, 100) crop:true, useSystemLocale:true];

    [cell.imageView sd_setImageWithURL:imageOptURL
                               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                               options:SDWebImageRefreshCached completed: nil];

    return cell;
}

D. 假设你有一个普通的ViewController(不是Collection/Table ViewController),你也还有一个IBOutlet imageView,并且想要从viewDidLoad方法中加载特定语言的图像

- (void)viewDidLoad {
    [super viewDidLoad];

    // Use the URL of imageSet obtained from imageOpt.com, this can be directly used or fetched from backend server
    NSString *imageURLString = @"https://p1.imageopt.net/9zt-ct/q";

    // Create an instance of NSLocale and intialize it to say french language
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr"];

    // Get the imageOpt url with query parameters to be used to fetch the image
    // Lets say this is a details type of view, so we will prefer not to crop the image, hence set crop to false
    [imageOptClient constructURLWithImageURL:imageURL imageView:self.imageView
                    crop:false locale:locale
        completionHandler: ^(NSURL* imageOptURL) {
    	        // Here we use SDWebImage to fetch, you can use any method of your choice,
    	        // just make sure standard cache control is enabled
              [self.imageView sd_setImageWithURL:imageOptURL
                                    placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
                                    options:SDWebImageRefreshCached completed: nil];
    }];
}

许可

版权所有 (c) 2019-20 imageOpt

在以下协议的条款下作为开源软件提供: MIT 许可协议