2011-07-11 26 views
2

我在傳真類型的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 

回答

7

我忘了,我看到這個代碼(也許在這裏SO) 我從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; 
} 
+1

我使用完全相同的方法。值得注意的是,儘管文檔中提到了['UIGraphicsBeginImageContext'](http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIGraphicsBeginImageContext)自iOS 4起[線程安全](http://www.cocoabuilder.com/archive/cocoa/296299-drawing-thread-safety-in-ios.html)。 –