2014-03-18 75 views
2

我目前拍攝照片時使用此驗證碼調整大小的UIImage從的UIImagePickerController

- (void) cameraButtonSelected 
{ 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

    [self presentViewController:picker animated:YES completion:NULL]; 
} 

我允許用戶編輯照片,但是當我使用這個委託方法由於某種原因的UIImagePickerController不會從刪除鑑於用戶後,按壓了「使用的照片」

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

我想知道

  1. 如何我從視圖中刪除後的UIImagePickerController「使用照片」按鈕按下
  2. 我如何調整我剛拍攝,因爲我需要一個較小的變種發送到我的服務器

任何幫助,將不勝感激的照片。

回答

4

簡單,你可以在你的- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法這裏查詢有關計算器

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
UIImage *tempImage=[info objectForKey:UIImagePickerControllerEditedImage]; 

[self.dealImageView setImage:[self imageWithImage:tempImage convertToSize:CGSizeMake(200, 200)]]; 

[self dismissViewControllerAnimated:YES completion:nil]; 

} 

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size { 

    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return destImage; 
} 

whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk

resizing-and-cropping-a-uiimage

1
  1. 呼叫[self dismissModalViewControllerAnimated:YES completion:nil]地方。這是任何模態視圖的常見做法。

  2. 這個問題可能比你想象的更復雜。在iOS中調整照片的方式有很多,您使用的代碼將取決於您的需求。不過,您可以查看此博客文章以瞭解照片大小調整。這是非常全面的,我建議您在編寫任何代碼之前閱讀它。

    http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

    話雖這麼說,這是一個非常簡單的方式實現調整:

    The simplest way to resize an UIImage?

5

您可以通過在信息字典查詢UIImagePickerControllerEditedImage獲得圖像。 並從視圖中刪除ImagePicker只需關閉選擇器。 這是調整大小的代碼。 只需用圖像實例調用它

您可以使用此功能將圖像縮放到特定大小

- (UIImage *) scaleImage:(UIImage*)image toSize:(CGSize)newSize { 
    //UIGraphicsBeginImageContext(newSize); 
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution). 
    // Pass 1.0 to force exact pixel size. 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

因此,最終的代碼應該是這樣的

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {  
    [picker dismissViewControllerAnimated:YES completion:Nil]; 
    UIImage *image = info[UIImagePickerControllerEditedImage]; 
    image = [self scaleImage:image toSize:CGSizeMake(200,200)]; // or some other size 
} 
相關問題