Reduced UIImage
Sometimes, you need to upload an image in a project. The image taken from the image library is too large, and it may take a lot of time to upload it, and it is not necessary to upload it too large, so I want to narrow down the picture and upload it again!
[Cpp]
# Pragma UIImagePickerDelagate
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingImage :( UIImage *) image editingInfo :( NSDictionary *) editingInfo
{
[Self. imagepovercontroller dismissPopoverAnimated: YES];
/*
After the selection is successful, it will be displayed on the Interface
*/
// Compress the image
Int iWidth = image. size. width;
Int iHeight = image. size. height;
If (iWidth> 300) {// 300 your custom size, how big it will be
IWidth = 300;
IHeight = image. size. height * iWidth/image. size. width;
If (iHeight & gt; 300 ){
IHeight = 300;
IWidth = image. size. width * iHeight/image. size. height;
}
}
Image = [self scaleToSize: image: CGSizeMake (iWidth, iHeight)]; // mainly here
NSData * imageData = UIImagePNGRepresentation (image );
[Self saveImage: imageData WithName: @ "pic.jpg"]; // Save the image
}
# Pragma UIImagePickerDelagate
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingImage :( UIImage *) image editingInfo :( NSDictionary *) editingInfo
{
[Self. imagepovercontroller dismissPopoverAnimated: YES];
/*
After the selection is successful, it will be displayed on the Interface
*/
// Compress the image
Int iWidth = image. size. width;
Int iHeight = image. size. height;
If (iWidth> 300) {// 300 your custom size, how big it will be
IWidth = 300;
IHeight = image. size. height * iWidth/image. size. width;
If (iHeight & gt; 300 ){
IHeight = 300;
IWidth = image. size. width * iHeight/image. size. height;
}
}
Image = [self scaleToSize: image: CGSizeMake (iWidth, iHeight)]; // mainly here
NSData * imageData = UIImagePNGRepresentation (image );
[Self saveImage: imageData WithName: @ "pic.jpg"]; // Save the image
}
[Cpp]
// Resize the image
-(UIImage *) scaleToSize :( UIImage *) image :( CGSize) newsize {
// Create a bitmap context
// Set it to the context currently in use
UIGraphicsBeginImageContext (newsize );
// Draw an image of varying size
[Image drawInRect: CGRectMake (0, 0, newsize. width, newsize. height)];
// Create an image with the size changed from the current context
UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext ();
// Make the current context output from the stack
UIGraphicsEndImageContext ();
// Returns a new image with a changed size.
Return scaledImage;
}
// Resize the image
-(UIImage *) scaleToSize :( UIImage *) image :( CGSize) newsize {
// Create a bitmap context
// Set it to the context currently in use
UIGraphicsBeginImageContext (newsize );
// Draw an image of varying size
[Image drawInRect: CGRectMake (0, 0, newsize. width, newsize. height)];
// Create an image with the size changed from the current context
UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext ();
// Make the current context output from the stack
UIGraphicsEndImageContext ();
// Returns a new image with a changed size.
Return scaledImage;
}