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

时间:2011-07-11 00:20:36

标签: objective-c ios uiimagepickercontroller

我在传真类型的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

1 个答案:

答案 0 :(得分:7)

我忘记了我看到这段代码的地方(也许就在这里)我从this thread获得了这段代码,并且在我从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;
}