2012-04-14 63 views
9

我正在使用iPad照片庫(相機膠捲)導入一些圖像,並將它們放入使用alloc方法創建的UIImageView中。但是當顯示圖像時會有很大的圖像質量損失。例如,在圖書館中,有一張圖像的河流非常銳利,逐個像素,但是當我輸入它時,它會丟失至少一半的像素。這是導入圖像的代碼:從iOS庫導入時導致顯着的圖像質量損失

- (IBAction) useCameraRoll: (id)sender 
{ 
    @autoreleasepool { 

if ([self.popoverController isPopoverVisible]) { 
    [self.popoverController dismissPopoverAnimated:YES]; 

} else{ 

    }{ 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypeSavedPhotosAlbum]) 
    { 
     @autoreleasepool { 

     UIImagePickerController *imagePicker = 
     [[UIImagePickerController alloc] init]; 

     imagePicker.delegate = self; 
     imagePicker.sourceType = 
     UIImagePickerControllerSourceTypePhotoLibrary; 
     imagePicker.mediaTypes = [NSArray arrayWithObjects: 
            (NSString *) kUTTypeImage, 
            nil]; 

     imagePicker.allowsEditing = YES; 

     self.popoverController = [[UIPopoverController alloc] 
            initWithContentViewController:imagePicker]; 
     } 
     popoverController.delegate = self; 

     [self.popoverController 
     presentPopoverFromBarButtonItem:sender 
     permittedArrowDirections:UIPopoverArrowDirectionUp 
     animated:YES]; 


     newMedia = NO; 


    }} 
} 
} 

-(void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self.popoverController dismissPopoverAnimated:true]; 
@autoreleasepool { 

NSString *mediaType = [info 
         objectForKey:UIImagePickerControllerMediaType]; 

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
    UIImage *image = [info 
         objectForKey:UIImagePickerControllerEditedImage]; 

    image1.image = image; 
    if (newMedia) 
     UIImageWriteToSavedPhotosAlbum(image, 
             self, 
              @selector(image:finishedSavingWithError:contextInfo:), 
             nil); 
} 
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) 
{ 
    // Code here to support video if enabled 
}} 
} 

-(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 
{ 

} 

,質量差(圖像放大),但你可以清楚地看到,即使在小規模的區別:

的iOS庫:

enter image description here

我的UIImageView:

enter image description here

回答

0

也許你應該使用

UIImage *img = [info valueForKey:@"UIImagePickerControllerOriginalImage"]; 

,以獲得原始圖像。

+0

你意思是代替UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage]; ?? – Alessandro 2012-04-18 18:51:29

+0

是的,作品非常好。即使我的這種方式也可以:UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage]; – Alessandro 2012-04-18 18:57:20

+0

...但沒有編輯選項,我想保留編輯版本! – Alessandro 2012-04-18 18:57:49

0

我在使用通用應用程序時發現了類似的問題。在iPod touch上使用allowEditing = YES從圖片相冊中挑選圖片效果良好,但是在iPad上,UIImagePickerControllerEditedImage只有320像素寬,導致質量下降,我想因爲用於編輯的彈出窗口只是320像素寬!

一番搜索後,我整個this code再次來到裁剪原始圖像使用從字典中的用戶裁剪設置,但我不禁感到我們有了一個更好的辦法來處理這在iPad上