首页 > 代码库 > ios-改变图片的尺寸

ios-改变图片的尺寸

//改变图片的尺寸

-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size

{

    UIGraphicsBeginImageContext(size);  //size 为CGSize类型,即你所需要的图片尺寸

    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImage *imagescaless=  [self imageRetain:scaledImage];

    return imagescaless;   //返回的就是已经改变的图片

}

//对.2x图片进行处理

-(UIImage*)imageRetain:(UIImage *)sourceImage

{

    CGSize size;

    size = CGSizeMake(sourceImage.size.width / 2.0f, sourceImage.size.height / 2.0f);

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);

    if (1.0 == [[UIScreen mainScreen] scale]){

        [sourceImage drawInRect:CGRectIntegral((CGRect){0.0f, 0.0f, size})];

    }else{

        [sourceImage drawInRect:(CGRect){0.0f, 0.0f, size}];

        sourceImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

    return sourceImage;

}

 

 

 

 

下面是调用方法:

- (void)viewDidLoad

{

    [super viewDidLoad];

    _dataList=[[NSMutableArray alloc]initWithCapacity:10];

    [self initViews];

    

    NSArray *tmpArray1 = @[@"照片墙",@"讨论区",@"群组",@"通讯录"];

    self.SXHAry = tmpArray1;

    

    NSArray *tmpArray2 = @[@"晚报放心购",@"厦门房产动态",@"厦门车市动态"];

    self.XMRBAry = tmpArray2;

    

    NSArray *bFirsts = @[@"gc1.png",@"gc2.png",@"gc3.png",@"gc4.png"];

    

    NSMutableArray *bFirst=[[NSMutableArray alloc]initWithCapacity:0];

    for (int i=0; i<bFirsts.count; i++) {

        UIImage*images=[UIImage imageNamed:[bFirsts objectAtIndex:i]];

        UIImage *imagenew=[self OriginImage:images scaleToSize:CGSizeMake(45, 45)];

        [bFirst addObject:imagenew];

    }

    self.bFirstAry = [bFirst copy];

    

    NSArray *bSecconds = @[@"gc6.png",@"gc7.png",@"gc8.png"];

    NSMutableArray *bSecond=[[NSMutableArray alloc]initWithCapacity:0];

    for (int i=0; i<bSecconds.count; i++) {

        UIImage*images=[UIImage imageNamed:[bSecconds objectAtIndex:i]];

        UIImage *imagenew=[self OriginImage:images scaleToSize:CGSizeMake(45, 45)];

        [bSecond addObject:imagenew];

    }

    self.bSecondAry = [bSecond copy];

    

    NSArray *rFirsts = @[@"gc1.png",@"gc2.png",@"gc3.png",@"gc4.png"];

    NSMutableArray *rFirst=[[NSMutableArray alloc]initWithCapacity:0];

    for (int i=0; i<rFirsts.count; i++) {

        UIImage*images=[UIImage imageNamed:[rFirsts objectAtIndex:i]];

        UIImage *imagenew=[self OriginImage:images scaleToSize:CGSizeMake(45, 45)];

        [rFirst addObject:imagenew];

    }

    self.rFirstAry = [rFirst copy];

    

    NSArray *rSeconds = @[@"gc6.png",@"gc7.png",@"gc8.png"];

    NSMutableArray *rSecond=[[NSMutableArray alloc]initWithCapacity:0];

    for (int i=0; i<rSeconds.count; i++) {

        UIImage*images=[UIImage imageNamed:[rSeconds objectAtIndex:i]];

        UIImage *imagenew=[self OriginImage:images scaleToSize:CGSizeMake(45, 45)];

        [rSecond addObject:imagenew];

    }

    self.rSecondtAry = [rSecond copy];

    [self setExtraCellLineHidden:self.squareTableCell];

}

 

ios-改变图片的尺寸