Objective c 如何减小使用相机拍摄的快照的文件大小?

Objective c 如何减小使用相机拍摄的快照的文件大小?,objective-c,ios,uiimagepickercontroller,Objective C,Ios,Uiimagepickercontroller,我在传真类型的iOS应用程序中使用相机,在这种情况下,图像往往会变得巨大,并导致传真web服务的明显延迟。我没有嫁给一个更好的图像质量。我的问题是如何降低分辨率/减小文件大小,使其成为更小的文件大小的图像 #import <UIKit/UIKit.h> //Definition of the delegate's interface @protocol SnapShotViewControllerDelegate -(void)selectedFileName:(NSString

我在传真类型的iOS应用程序中使用相机,在这种情况下,图像往往会变得巨大,并导致传真web服务的明显延迟。我没有嫁给一个更好的图像质量。我的问题是如何降低分辨率/减小文件大小,使其成为更小的文件大小的图像

#import <UIKit/UIKit.h>

//Definition of the delegate's interface
@protocol SnapShotViewControllerDelegate

-(void)selectedFileName:(NSString*)filename;

@end




@interface SnapShotViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate> {

    IBOutlet UIButton *button;
    IBOutlet UIImageView *image;
    UIImagePickerController *imgPicker;

}

- (IBAction)grabImage;
- (IBAction)useImage;

@property (nonatomic, retain) UIImagePickerController *imgPicker;
@property (nonatomic,retain)UIImageView *image;
@property (nonatomic,retain)UIButton *button;
@property (nonatomic, assign) id<SnapShotViewControllerDelegate> delegate;

@end




#import "SnapShotViewController.h"
#import "PDFImageConverter.h"


@implementation SnapShotViewController

@synthesize imgPicker, image, button;
@synthesize delegate=_delegate;


- (IBAction)grabImage{

    [self presentModalViewController:self.imgPicker animated:YES];

}

- (IBAction)useImage{

    if ([image image]==nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Image Found" message:@"Click 'Grab Image' to take a snapshot first" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];   
        [alert show];
        [alert release];
    }else {

        //do something with file



        [self.delegate selectedFileName: filename];

        [self.navigationController popViewControllerAnimated:YES];
    }


}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
    [image setImage:img];
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];


}




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    if (imgPicker == nil) {
        self.imgPicker = [[[UIImagePickerController alloc] init] autorelease];
    }

    //self.imgPicker.allowsImageEditing = YES;
    imgPicker.delegate =self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {

        self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    }else

    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.image = nil;
    self.button = nil;


}


- (void)dealloc {

    [imgPicker release];
    [image release];
    [button release];

    [super dealloc];
}


@end
#导入
//委托接口的定义
@协议快照ViewControllerDelegate
-(void)selectedFileName:(NSString*)文件名;
@结束
@界面SnapShotViewController:UIViewController{
IBUIButton*按钮;
IBUIImageView*图像;
UIImagePickerController*imgPicker;
}
-(IBAction)抓取图像;
-(i)使用图像;
@属性(非原子,保留)UIImagePickerController*imgPicker;
@属性(非原子,保留)UIImageView*图像;
@属性(非原子,保留)UIButton*按钮;
@属性(非原子,赋值)id委托;
@结束
#导入“SnapShotViewController.h”
#导入“PDFImageConverter.h”
@SnapShotViewController的实现
@综合imgPicker、图像、按钮;
@综合委托=_委托;
-(IBAction)抓取图像{
[self-presentModalViewController:self.imgPicker动画:是];
}
-(iAction)使用图像{
如果([image]==nil){
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“找不到图像”消息:@“单击“抓取图像”先拍摄快照”委托:nil取消按钮提示:@“确定”其他按钮提示:nil];
[警报显示];
[警报发布];
}否则{
//用文件做些什么
[self.delegate selectedFileName:filename];
[self.navigationController PopViewControllerInitiated:是];
}
}
-(无效)imagePickerController:(UIImagePickerController*)PickerDidFinishPickingImage:(UIImage*)img编辑信息:(NSDictionary*)编辑信息{
[图像设置图像:img];
[[picker parentViewController]解除ModalViewController激活:是];
}
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
如果(imgPicker==nil){
self.imgPicker=[[UIImagePickerController alloc]init]autorelease];
}
//self.imgPicker.allowsImageEdit=是;
imgPicker.delegate=self;
如果([UIImagePickerController IsSourceType可用:UIImagePickerController SourceType相机]){
self.imgPicker.sourceType=UIImagePickerControllerSourceTypeCamera;
}否则
self.imgPicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
-(无效)视图卸载{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
self.image=nil;
self.button=nil;
}
-(无效)解除锁定{
[imgPicker释放];
[图片发布];
[按钮释放];
[super dealoc];
}
@结束
我忘了我在哪里看到这段代码(可能就在这里),我是从哪里得到这段代码的,当我从我的iPhone相机中获得图像后,我在我的一个应用程序中使用了这段代码:

CGSize newImageSize = CGSizeMake(100, 133);
    self.studentPic.image = student.pic = [StudentInfo imageWithImage:[info objectForKey:UIImagePickerControllerOriginalImage] scaledToSize:newImageSize];

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // End the context
    UIGraphicsEndImageContext();

    // Return the new image.
    return newImage;
}

我使用完全相同的方法。值得注意的是,不管文档怎么说,从iOS 4开始。