Shrink image without affecting the quality in objective c

后端 未结 2 731
小鲜肉
小鲜肉 2021-02-06 10:52

How to shrink the image without affecting the quality programmatically.

After capture the image I want to reduce the size of that image without changing the quality in

2条回答
  •  悲&欢浪女
    2021-02-06 11:27

    I search a lot with this topic a lot. After few days, i found this one. Hope this is much faster then the accepted answer.

    NSData *imageData;
    imageData=[[NSData alloc] initWithData:UIImageJPEGRepresentation((chosenImage), 1.0)];
    
    NSLog(@"[before] image size: %lu--", (unsigned long)[imageData length]);
    
    CGFloat scale= (100*1024)/(CGFloat)[imageData length]; // For 100KB. 
    
    
    UIImage *small_image=[UIImage imageWithCGImage:chosenImage.CGImage scale:scale orientation:chosenImage.imageOrientation];
    
    imageData = UIImageJPEGRepresentation(small_image, scale*1.00);
    
    
    NSLog(@"[after] image size: %lu:%f", (unsigned long)[imageData length],scale);
    

    It worked for me great!. Try it once.

提交回复
热议问题