2017-12-05 56 views
1

UIImageView를 가운데에 놓고 확대/축소를 수행하려고합니다. 아래 코드에 따라 확대/축소는 정상적으로 작동합니다. 하지만 내 UIImageView NavigationBar 아래에서 시작합니다. 하지만 UIImageView 센터 및 또한 센터 확대 및 중앙에 UIScrollView contentSize 확대합니다.Xamarin.iOS의 UIScrollView에 확대/축소 이미지 센터를 배치하십시오.

코드 : 위의 코드 줌에서

scrollView = new UIScrollView(); 
scrollView.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Height - 20.0f); 
View.AddSubview(scrollView); 

dicom_image = new UIImageView(new CGRect(0, 0, View.Frame.Size.Width,300)); 
dicom_image.Image = dicomItem.uiimage; 

dicom_image.UserInteractionEnabled = true; 

if (dicomItem.uiimage != null) 
{ 
    dicom_image.Image = ExtensionMethods.MaxResizeImage(dicomItem.uiimage, (float)UIScreen.MainScreen.Bounds.Width, (float)UIScreen.MainScreen.Bounds.Width); 
    dicom_image.ClipsToBounds = true; 
    dicom_image.TranslatesAutoresizingMaskIntoConstraints = false; 
    dicom_image.SizeToFit(); 
}; 
dicom_image.Center = scrollView.Center; 


scrollView.ContentSize = scrollView.Frame.Size; 
scrollView.AddSubview(dicom_image); 

scrollView.MaximumZoomScale = 3f; 
scrollView.MinimumZoomScale = 1f; 
scrollView.ViewForZoomingInScrollView += (UIScrollView sv) => { return dicom_image; }; 

는 잘 작동하지만 난 중심에있는 UIImageView 싶다.

편집 :

이 코드는 확대에 대한 잘 작동하지만, 이미지의 시작이 아닌 중심을 처음부터입니다.

scrollView = new UIScrollView(); 
          scrollView.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Height - 20.0f); 
          View.AddSubview(scrollView); 

          dicom_image = new UIImageView(new CGRect(0, 0, View.Frame.Size.Width,300)); 
          dicom_image.Image = dicomItem.uiimage; 
             dicom_image.ContentMode = UIViewContentMode.Center; 

          dicom_image.UserInteractionEnabled = true; 

          if (dicomItem.uiimage != null) 
          { 
           dicom_image.Image = ExtensionMethods.MaxResizeImage(dicomItem.uiimage, (float)UIScreen.MainScreen.Bounds.Width, (float)UIScreen.MainScreen.Bounds.Width); 
           dicom_image.ClipsToBounds = true; 
           dicom_image.TranslatesAutoresizingMaskIntoConstraints = false; 
           dicom_image.SizeToFit(); 
          }; 

          dicom_image.Center = scrollView.Center; 


          scrollView.ContentSize = dicom_image.Frame.Size; 
          scrollView.AddSubview(dicom_image); 

       scrollView.ShowsVerticalScrollIndicator = false; 
       scrollView.ShowsHorizontalScrollIndicator = false; 

       scrollView.MaximumZoomScale = 3f; 
       scrollView.MinimumZoomScale = 1f; 
       scrollView.ViewForZoomingInScrollView += (UIScrollView sv) => { return dicom_image; }; 

       UITapGestureRecognizer doubletap = new UITapGestureRecognizer(OnDoubleTap) 
       { 
        NumberOfTapsRequired = 2 // double tap 
       }; 
       scrollView.AddGestureRecognizer(doubletap); 

감사합니다.

답변

1

제약 조건을 사용하지 않으므로 UIImageView의 범위를 설정할 수 있습니다.

이것은 Instagram 뷰어에 사용 된 이미지의 크기를 조절하기 위해 CIFilter을 사용하는 스트라이프 다운 예제입니다.

//someUIImageInstance is a CGImage-backed UIImage 
var xScale = (float)(View.Frame.Size.Width/someUIImageInstance.CGImage.Width); 
var yScale = (float)(View.Frame.Size.Height/someUIImageInstance.CGImage.Height); 
var scale = Math.Max(xScale, yScale); 
var lanczosScaleTransform = new CILanczosScaleTransform 
{ 
    Image = someUIImageInstance.CGImage, 
    Scale = scale 
}; 
var yPosition = (scale * View.Frame.Size.Height)/2; 
imageView = new UIImageView(new CGRect(0, 0, View.Frame.Size.Width, View.Frame.Size.Height)) 
{ 
    Image = UIImage.FromImage(lanczosScaleTransform.OutputImage), 
    Bounds = new CGRect(0, yPosition, View.Frame.Size.Width, View.Frame.Size.Height - yPosition), 
    UserInteractionEnabled = true, 
}; 
scrollView = new UIScrollView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height)) 
{ 
    MinimumZoomScale = 1f, 
    MaximumZoomScale = 3f, 
    BackgroundColor = UIColor.LightGray 
}; 
scrollView.ViewForZoomingInScrollView = (scrollView) => { return imageView; }; 

scrollView.AddSubview(imageView); 
View.AddSubview(scrollView); 

enter image description here

참고 : UIImageView을 중심으로 이미지가 확대 될 때 뷰포트의 중심을 제어 측면에서 제약을 사용하는 경우는 ... 쉽게 내가 중심을 설정하는 방법을

+0

사용자가 손가락으로 줌하면 구속 조건을 사용하여 이미지. – Ironman

+0

@Ironman을 사용하면 scrollview의 델리게이트 (scrollViewDidZoom :')를 통해 동적으로 조절할 수 있습니다. 또 다른 메모로, 'CIImage'를 텍스처로 렌더링하기 위해 MTKView를 사용하면 속도가 매우 빠르며 CIFilter와 뷰를 GPU에 직접 연결할 수 있습니다. 스크롤링/zomming은 실크처럼 부드럽습니다. 화면에 표시되는 CIImage 범위 만 렌더링합니다. 즉, 원래 이미지의 픽셀 화는 없습니다. – SushiHangover