• 攻略 10-4: 缩放图像


    -(void)configureDetailsWithImage:(UIImage *)image label:(NSString *)label showsButtons:(BOOL)showsButton
    {
        self.imageView.image = image;
        self.detailDescriptionLabel.text = label;
        if (showsButton == NO)
        {
            self.selectImageButton.hidden = YES;
            self.clearImageButton.hidden = YES;
        }
        else if (showsButton == YES)
        {
            self.selectImageButton.hidden = NO;
            self.clearImageButton.hidden = NO;
        }
    }
    -(void)setMainImage:(UIImage *)image
    {
        [self.filteredImages removeAllObjects];
        if (image != nil)
        {
            [self populateImageViewWithImage:image];
        }
        
        _mainImage = image;
        NSIndexPath *currentIndexPath = self.tableView.indexPathForSelectedRow;
        [self.tableView reloadData];
        [self.tableView selectRowAtIndexPath:currentIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    }
     1 + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size
     2 {
     3     UIGraphicsBeginImageContext(size);
     4     [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
     5     UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
     6     UIGraphicsEndImageContext();
     7     return scaledImage;
     8 }
     9 
    10 + (UIImage *)aspectScaleImage:(UIImage *)image toSize:(CGSize)size
    11 {
    12     if (image.size.height < image.size.width)
    13     {
    14         float ratio = size.height / image.size.height;
    15         CGSize newSize = CGSizeMake(image.size.width * ratio, size.height);
    16         
    17         UIGraphicsBeginImageContext(newSize);
    18         [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    19     }
    20     else
    21     {
    22         float ratio = size.width / image.size.width;
    23         CGSize newSize = CGSizeMake(size.width, image.size.height*ratio);
    24         
    25         UIGraphicsBeginImageContext(newSize);
    26         [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    27     }
    28     UIImage *aspectScaledImage = UIGraphicsGetImageFromCurrentImageContext();
    29     UIGraphicsEndImageContext();
    30     return aspectScaledImage;
    31 }
    32 
    33 + (UIImage *)aspectFillImage:(UIImage *)image toSize:(CGSize)size
    34 {
    35     UIGraphicsBeginImageContext(size);
    36     if (image.size.height< image.size.width)
    37     {
    38         float ratio = size.height/image.size.height;
    39         [image drawInRect:CGRectMake(0, 0, image.size.width*ratio, size.height)];
    40     }
    41     else
    42     {
    43         float ratio = size.width/image.size.width;
    44         [image drawInRect:CGRectMake(0, 0, size.width, image.size.height*ratio)];
    45     }
    46     UIImage *aspectScaledImage = UIGraphicsGetImageFromCurrentImageContext();
    47     UIGraphicsEndImageContext();
    48     return aspectScaledImage;
    49 }
     1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     2 {
     3     if (self.mainImage != nil)
     4     {
     5         UIImage *image;
     6         NSString *label;
     7         BOOL showsButtons = NO;
     8         if (indexPath.row == 0)
     9         {
    10             image = self.mainImage;
    11             label = @"Select an Image to Display";
    12             showsButtons = YES;
    13         }
    14         else if (indexPath.row == 1)
    15         {
    16             image = [MasterViewController scaleImage:self.mainImage toSize:self.detailViewController.imageView.frame.size];
    17             label = @"Chosen Image Resized";
    18         }
    19         else if (indexPath.row == 2)
    20         {
    21             image = [MasterViewController aspectScaleImage:self.mainImage toSize:self.detailViewController.imageView.frame.size];
    22             label = @"Chosen Image Scaled";
    23         }
    24         else if (indexPath.row == 3)
    25         {
    26             image = [self.filteredImages objectAtIndex:0];
    27             CGSize contentSize = self.detailViewController.imageView.frame.size;
    28             image = [MasterViewController aspectScaleImage:image toSize:contentSize];
    29             label = @"Hue Adjustment";
    30         }
    31         else if (indexPath.row == 4)
    32         {
    33             image = [self.filteredImages objectAtIndex:1];
    34             CGSize contentSize = self.detailViewController.imageView.frame.size;
    35             image = [MasterViewController aspectScaleImage:image toSize:contentSize];
    36             label = @"Straightening Filter";
    37         }
    38         else if (indexPath.row == 5)
    39         {
    40             image = [self.filteredImages objectAtIndex:2];
    41             CGSize contentSize = self.detailViewController.imageView.frame.size;
    42             image = [MasterViewController aspectScaleImage:image toSize:contentSize];
    43             label = @"Series Filter";
    44         }
    45         [self.detailViewController configureDetailsWithImage:image label:label showsButtons:showsButtons];
    46     }
    47 }
  • 相关阅读:
    吴恩达《深度学习》第四门课(3)目标检测(Object detection)
    cnn知识点汇总
    吴恩达《深度学习》第四门课(2)卷积神经网络:实例探究
    吴恩达《深度学习》第四门课(1)卷积神经网络
    解决Eclipse中springBoot中文乱码问题
    SpringBoot返回页面乱码解决
    【Maven】---Linux搭建Nexus3.X私服
    ASP.NET中TextBox控件设立ReadOnly="true"后台取不到值
    Winform打包工具SetupFactory 9 的使用
    PostgreSQL
  • 原文地址:https://www.cnblogs.com/fengmin/p/5578393.html
Copyright © 2020-2023  润新知