2016-05-19 75 views
0

我正在嘗試瀏覽圖像並使用UIImagePickerControllerimageview中顯示。基本上我用來瀏覽圖像的視圖基於navigationController導航視圖控制器中的IOS圖像選取器

但是,在選擇圖像並回來查看後,使用下面的代碼,我可以看到視圖閃爍到左側,圖像未在圖像視圖中顯示。

實際上整個源代碼都退出很長時間,這就是爲什麼我只發佈圖像選擇器部分。如果需要,我可以發佈更多的代碼。

RegistrationFormViewController.h

@interface RegistrationFormViewController : ViewController 
<UIImagePickerControllerDelegate,UINavigationControllerDelegate,CLLocationManagerDelegate> 
{ 
    CLLocationManager *locationManager; 
    UIImagePickerController *imagePickerController; 
} 
@property (strong, nonatomic) CLLocationManager *locationManager; 
@property (weak, nonatomic) IBOutlet UIImageView *imageUser; 

- (IBAction)registerBt:(id)sender; 

RegistrationFormViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    _barButtonBack.target = self.revealViewController; 
    _barButtonBack.action = @selector(revealToggle:); 
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 


    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)]; 
    singleTap.numberOfTapsRequired = 1; 
    [self.imageUser setUserInteractionEnabled:YES]; 
    [self.imageUser addGestureRecognizer:singleTap]; 
} 



-(void)tapDetected{ 
    NSLog(@"single Tap on imageview"); 
    imagePickerController = [[UIImagePickerController alloc]init]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 
} 

#pragma mark - ImagePickerController Delegate 

- (void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image 
        editingInfo:(NSDictionary *)editingInfo 
{ 
    // Dismiss the image selection, hide the picker and 

    //show the image view with the picked image 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
    [self.imageUser setImage:image]; 

    self.imageUser.contentMode = UIViewContentModeScaleAspectFill; 
} 

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
} 
+1

爲什麼你在'didFinishPickingImage'回調更新'self.imageUser.contentMode'? – ozgur

+0

其實我對ios很陌生,做了導致問題的原因,我已經評論過這個部分,但其行爲仍然是一樣的。 – Haris

+1

至少先更新內容模式,然後設置圖像,最後關閉選取器。 – ozgur

回答

1

。在你的代碼,你在這裏沒有顯示的問題。然而嘗試這種在didFinishPickingImage -

dispatch_async(dispatch_get_main_queue(), ^{ 
    self.imageUser.contentMode = UIViewContentModeScaleAspectFill; 
    [self.imageUser setImage:image]; 

    }); 

這個代碼移到viewDidLoad中

imagePickerController = [[UIImagePickerController alloc]init]; 
    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

您還可以檢查您是否得到圖像或者不使用斷點。

1

在viewcontroller.m文件只要把代碼中,我希望這將是有益的視圖 - 控制

#import <AssetsLibrary/AssetsLibrary.h> 
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset); 
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error); 
- (IBAction)Gallery:(id)sender { 

self.ImagePickerController = [[UIImagePickerController alloc]init]; 
self.ImagePickerController.delegate = self; 
self.ImagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentViewController:self.ImagePickerController animated:YES completion:nil]; 

}

- (IBAction)Camera:(id)sender { 

self.ImagePickerController = [[UIImagePickerController alloc]init]; 
self.ImagePickerController.delegate = self; 
self.ImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
[self presentViewController:self.ImagePickerController animated:YES completion:nil]; 

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 
{ 
    [picker dismissViewControllerAnimated:YES completion:nil]; 


NSURL *imageUrl = (NSURL *)[info objectForKey:UIImagePickerControllerReferenceURL]; 
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
{ 
    ALAssetRepresentation *representation = [myasset defaultRepresentation]; 
    CGImageRef resolutionRef = [representation fullResolutionImage]; 

    if (resolutionRef) { 
     UIImage *image = [UIImage imageWithCGImage:resolutionRef scale:1.0f orientation:(UIImageOrientation)representation.orientation]; 
     self.SelectedImage.image =image; 

    } 
}; 
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
{ 
    NSLog(@"cant get image - %@",[myerror localizedDescription]); 
}; 

if(imageUrl) 
{ 
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc]init]; 
    [assetslibrary assetForURL:imageUrl resultBlock:resultblock failureBlock:failureblock]; 
} 

}

。 h文件

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate> 
@property (nonatomic)UIImagePickerController *ImagePickerController; 
@property (weak, nonatomic) IBOutlet UIImageView *SelectedImage; 
1

試試這個委託方法

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

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage]; 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 
相關問題