IOS Pictures to Nsdata-b

Source: Internet
Author: User

iOS development uiimage may often need to switch to NSData upload delivery, etc.

There are two more commonly used methods

    1. UIImageJPEGRepresentation

    1. Uiimagepngrepresentation


The first method has two parameters

    1. UIImageJPEGRepresentation (UIImage * __nonnull image, CGFloat compressionquality);

The first parameter is the picture, the second parameter is the need to set the definition, the value is between 0 to 1, the larger the value is clear, occupy more space, conversely, the lower the value of the less clarity, the smaller the space occupied

We're directly on the code.

Bar two ways to make a comparison

We create a project, call the system's album or the camera to take a picture

First to convert a high-definition picture (here we put the important code, the other code we will upload to QQ group space, see the bottom of the article)

    1. UIImage *image;
    2. Image=[info Valueforkey:uiimagepickercontrolleroriginalimage];
    3. ImageData = uiimagejpegrepresentation (image,1);
    4. float length = [imageData length]/1024;
    5. NSLog (@ "image length===%f", length);



In the first way to turn a lower-definition picture

    1. ImageData1 = uiimagejpegrepresentation (image,0. 1);
    2. Float length1 = [imageData1 length]/1024;
    3. NSLog (@ "image length1===%f", length1);


And then the second way to convert a single

 
    1. ImageData2 = uiimagepngrepresentation (image);
    2. Float length2 = [ImageData2 length]/1024;
    3. NSLog (@ "image length===%f", length2);


We'll find out by executing code

  1. testimagetodata[13 17:49< Span class= "Xcodenumber" >4705]  image length===297< Span class= "xcodeconstants" >.000000&NBSP;&NBSP;
  2. testimagetodata[1317:494705] image length1===34.000000   
  3. testimagetodata[1317:494705] image length===19 0. 000000

The same picture is converted in three ways after the size difference compare hit

First way high quality picture 297Kb low quality picture 34Kb

The second way out of the 190Kb

=================

The SDK provides methods to directly invoke the

UIImage *img = [UIImage imagenamed:@ "Some.png"];

NSData *dataobj = UIImageJPEGRepresentation (IMG, 1.0);

The following is the contents of the UIImage.h header file in the SDK

Uikit_extern nsdata *uiimagepngrepresentation (UIImage *image);

return image as PNG. May return nil if image have no cgimageref or invalid bitmap format

Uikit_extern nsdata *uiimagejpegrepresentation (UIImage *image, CGFloat compressionquality);

return image as JPEG. May return nil If image has no cgimageref or invalid bitmap format. Compression is 0 (most): 1 (least)

JPEG conversion method inside the second parameter is the compression coefficient, can effectively reduce the size of the picture. Uiimagepngrepresentation (uiimage* image) is much larger than the amount of image data returned by uiimagejpegrepresentation (uiimage* image, 1.0). Before uploading the pictures in the project, the PNG size of the same picture was tested at 8M, while the JPG compression factor was 0.75, and the size was only 1 m. Also, reducing the compression factor does not have much effect on the picture visually.

==========================

A simple comparison of image compression using uiimagejpegrepresentation and UigraphicsbeginimagecontextUIImage *image01 = [UIImage imageNamed:@"002.png"];//原图02. 03.NSData *dataOf02 =UIImageJPEGRepresentation(image01, 0.5);//压缩图片内容,不影响图片的size,得到一个原大小,但更模糊的图片。04.UIImage *image02 = [UIImage imageWithData:dataOf02];//压缩图0205. 06.UIImage *image03 = [self makeThumbnailFromImage:image01 scale:0.5];//压缩图0307.NSLog(@"001===%@",NSStringFromCGSize(image01.size));08.NSLog(@"002===%@",NSStringFromCGSize(image02.size));09.NSLog(@"003===%@",NSStringFromCGSize(image03.size));10. 11.NSData *data001 = UIImageJPEGRepresentation(image01, 1.0);12.NSData *data002 = UIImageJPEGRepresentation(image02, 1.0);13.NSData *data003 = UIImageJPEGRepresentation(image03, 1.0);14. 15.NSLog(@"002.lenght===%d",data001.length);16.NSLog(@"002.lenght===%d",data002.length);17.NSLog(@"003.lenght===%d",data003.length);18. 19.//看一下效果20.UIImageView *imageView01 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image01.size.width, image01.size.height)];21.imageView01.image = image01;22.[self.view addSubview:imageView01];23. 24.UIImageView *imageView02 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, image02.size.width, image02.size.height)];25.imageView02.image = image02;26.[self.view addSubview:imageView02];27. 28.UIImageView *imageView03 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, image03.size.width, image03.size.height)];29.imageView03.image = image03;30.[self.view addSubview:imageView03];

View Sourceprint?01.//手动实现图片压缩,可以写到分类里,封装成常用方法。按照大小进行比例压缩,改变了图片的size。02.- (UIImage *)makeThumbnailFromImage:(UIImage *)srcImage scale:(double)imageScale {03.UIImage *thumbnail = nil;04.CGSize imageSize = CGSizeMake(srcImage.size.width * imageScale, srcImage.size.height * imageScale);05.if (srcImage.size.width != imageSize.width || srcImage.size.height != imageSize.height)06.{07.UIGraphicsBeginImageContext(imageSize);08.CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);09.[srcImage drawInRect:imageRect];10.thumbnail = UIGraphicsGetImageFromCurrentImageContext();11.UIGraphicsEndImageContext();12.}13.else14.{15.thumbnail = srcImage;16.}17.return thumbnail;18.}

IOS Pictures to Nsdata-b

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.