如何在目标 c 中减小图像的大小
Posted
技术标签:
【中文标题】如何在目标 c 中减小图像的大小【英文标题】:How to decrease the size of the Image in objective c 【发布时间】:2012-09-26 10:32:32 【问题描述】:问题是,当图像大小超过 60 kb 时,它不会将图像发布到 web 服务,但如果图像大小小于 60 kb,则将其发布到 web 服务。
如何减小目标 c 中图像的大小。 以下是我正在使用的代码,
-(IBAction)sendEmail:(id)sender
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"TABLEDIC%@",appDelegate.tableDic);
//Parsing
recordResults = FALSE;
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<CreateTextMail xmlns=\"http://tempuri.org/\">\n"
"<listid>%@</listid>\n"
"<fromid>%@</fromid>\n"
"<subject>%@</subject>\n"
"<replyto>%@</replyto>\n"
"<loginid>%@</loginid>\n"
"<fromname>%@</fromname>\n"
"<forward>%@</forward>\n"
"<subscribe>%@</subscribe>\n"
"<mailpriority>%@</mailpriority>\n"
"<recievermailtext>%@</recievermailtext>\n"
"<mailbody>%@</mailbody>\n"
"<emailname>%@</emailname>\n"
"<signature>%@</signature>\n"
"<listname>%@</listname>\n"
"<emailtype>%@</emailtype>\n"
"<imagecontent>%@</imagecontent>\n"
"<imagename>%@</imagename>"
"</CreateTextMail>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",[appDelegate.tableDic valueForKey:@"tableID"],[appDelegate.tableDic valueForKey:@"fromname"],[appDelegate.tableDic valueForKey:@"subject"],[appDelegate.tableDic valueForKey:@"replyto"],[appDelegate.tableDic valueForKey:@"loginid"],[appDelegate.tableDic valueForKey:@"fromname"],forward.text,subscribe.text,[appDelegate.tableDic valueForKey:@"mailpriority"],receivermailtext.text,body.text,[appDelegate.tableDic valueForKey:@"emailName"],[appDelegate.tableDic valueForKey:@"signature"],[appDelegate.tableDic valueForKey:@"tableName"],emailType,strEncoded,imageName.text
];
NSLog(@"SOPA%@",soapMessage);
NSURL *url = [NSURL URLWithString:@"http://www.xxx.net/xxx/xxx.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/CreateTextMail" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code : %d",[urlResponse statusCode]);
if([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
NSLog(@"Response: %@",result);
if( theConnection )
webData = [NSMutableData data];
else
NSLog(@"theConnection is NULL");
[subject resignFirstResponder];
[replyto resignFirstResponder];
[loginid resignFirstResponder];
[fromname resignFirstResponder];
[emailName resignFirstResponder];
[signature resignFirstResponder];
[listName resignFirstResponder];
-(void)takeCamera
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
NSLog(@"take a photo");
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = (id)self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
newMedia = YES;
-(void)pickCameraRoll
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = (id)self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
newMedia = NO;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if([mediaType isEqualToString:(NSString *)kUTTypeImage])
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
NSData *myData = UIImagePNGRepresentation(image);
// NSLog(@"NSData : %@",myData);
[Base64 initialize];
strEncoded = [Base64 encode:myData];
NSLog(@"strEncoded : %@",strEncoded);
if(newMedia)
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:finishedSavingWithError:contextInfo:), nil);
NSLog(@"image pick");
else if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
-(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
if(error)
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Save Failed" message:@"Failed to save image" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self dismissModalViewControllerAnimated:YES];
设备会出现什么问题,无法将图像发送到 Web 服务,但能够在 iphone 模拟器中成功发送。 任何建议都会非常有帮助。 提前致谢。
【问题讨论】:
从哪里设置或获取此行中指定的图像名称 ....valueForKey:@"tableName"],emailType,strEncoded,imageName.text ]; 文本字段中的一些值和字典中的一些值。 imageName.text 来自 textField right.Do you type the image name in imageName text Field or you are retreiving it from other things 检查设备的网络连接.. 允许用户在文本字段中输入图像名称。 【参考方案1】:使用以下代码解压缩图像,它会循环直到图像大小减小到最大图像大小。
CGFloat maxCompressionFactor = 0.1f;
CGFloat compressionFactor = 0.9f;
int maxImageSize = 60 * 1024;
NSData *imageData = UIImageJPEGRepresentation(image, compressionFactor);
while ([imageData length] > maxImageSize && compressionFactor > maxCompressionFactor)
compressionFactor -= 0.1;
imageData = UIImageJPEGRepresentation(image, compressionFactor);
【讨论】:
【参考方案2】:可以通过增加压缩来减小图像的大小,但会牺牲图像质量。
使用:UIImageJPEGRepresentation
UIImage *compressedImage = UIImageJPEGRepresentation(theImage, 0.5);
【讨论】:
其中 0.5 是压缩率。它的范围是0-1。 0 是最大压缩率(最低质量)。以上是关于如何在目标 c 中减小图像的大小的主要内容,如果未能解决你的问题,请参考以下文章