2016-06-14 69 views
0

我的UIScrollView上有UIImageView。您可以縮放它並裁剪可見部分。捕捉UIScrollView的可見部分後圖像的透明部分

這裏是我的卡代碼:

UIImage *visibleScrollImage = nil; 

    UIGraphicsBeginImageContextWithOptions(self.scrollView.bounds.size, YES, [UIScreen mainScreen].scale); 
    { 
     CGPoint offset = self.scrollView.contentOffset; 
     CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y); 
     [self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     visibleScrollImage = UIGraphicsGetImageFromCurrentImageContext(); 
    } 
    UIGraphicsEndImageContext(); 

    eivc.editImage = visibleScrollImage; 

當圖像不走的UIScrollView的所有可見的部分,我不知道如何捕捉圖像,而UIImageView的透明部分。我試圖用上下文和偏移座標的不同尺寸來猜測,但它沒有奏效,因爲我顯然不理解有關圖像,scrollview或/和CGContext的內容。

這裏有一些例子,我設置backgroundColor紅色的可見性。

What i get right now

What i want

編輯:

現在我做了一半的解決方案 我設置imageView.frame這樣

self.imageView.contentMode = UIViewContentModeScaleAspectFill; 
     CGFloat initialImageHeight = result.size.height; 
     CGFloat initialImageWidth = result.size.width; 
     CGFloat height = self.scrollView.bounds.size.height; 
     CGFloat width = self.scrollView.bounds.size.width; 
     CGFloat frameHeight = height; 
     CGFloat frameWidth = width; 

     if ((initialImageHeight/initialImageWidth) > (height/width)) { 
      frameHeight = width * initialImageHeight/initialImageWidth; 
     } else if ((initialImageHeight/initialImageWidth) < (height/width)) { 
      frameWidth = height * initialImageWidth/initialImageHeight; 
     } 

     self.imageView.frame = CGRectMake(0, 0, frameWidth, frameHeight); 
     self.imageView.image = result; 

和我做了minimumZoomScale = 1.0

它阻止圖像採取不完整的滾動視圖可見部分。

回答

0

由於你的描述,我想你的需求,如enter image description here

enter image description here

第一圖像是正常的,第二圖像縮放圖像。讓你感到困惑的是,在沒有imageView透明部分的情況下捕捉圖像,所以當你放大imageView時,imageView的原點和邊界會隨之變化。你可以獲取屏幕圖像,然後基於imageView的座標,計算出一個矩形,這是一個imageView,一個簡短的圖像,這個圖像是你的imageView的圖像,代碼如下。

- (UIImage*)cropImage{ 
    UIImage *screenImage = [UIImage imageFromView:self]; 
    CGFloat imageViewWidth = self.imageView.frame.size.width; 
    CGFloat imageViewHeight = self.imageView.frame.size.height; 

    CGRect rect; 
    UIImage *clipImage; 

    if (type == 1) { 
     if (imageViewHeight < SCREENWIDTH) { 

      rect = CGRectMake(0, SCREENHEIGHT/2-imageViewHeight/2, SCREENWIDTH, imageViewHeight); 
     }else{ 

      rect = CGRectMake(0, SCREENHEIGHT/2-SCREENWIDTH/2, SCREENWIDTH, SCREENWIDTH); 
     } 
    }else if (type == 2){ 
     if (imageViewWidth < SCREENWIDTH) { 
      rect = CGRectMake(SCREENWIDTH/2-imageViewWidth/2, SCREENHEIGHT/2-SCREENWIDTH/2, imageViewWidth, SCREENWIDTH); 
     }else{ 

      rect = CGRectMake(0, SCREENHEIGHT/2-SCREENWIDTH/2, SCREENWIDTH, SCREENWIDTH); 
     } 
    }else{ 
      rect = CGRectMake(0, SCREENHEIGHT/2-SCREENWIDTH/2, SCREENWIDTH, SCREENWIDTH); 
    } 
    clipImage = [screenImage cropImage:screenImage inRect:rect]; 
    return clipImage; 
} 


+ (UIImage *) imageFromView:(UIView *)imageView { 
    CGFloat scale = 1; 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES)  { 
    scale = [[UIScreen mainScreen] scale]; 
    } 
    if (scale > 1) { 
     UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, scale); 
    } else { 
     UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 2); 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [imageView.layer renderInContext: context]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return viewImage; 
} 


- (UIImage *)cropImage:(UIImage*)image inRect:(CGRect)rect 
{ 
    double (^rad)(double) = ^(double deg) { 
     return deg/180.0 * M_PI; 
    }; 

CGAffineTransform rectTransform; 
switch (image.imageOrientation) { 
    case UIImageOrientationLeft: 
     rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height); 
     break; 
    case UIImageOrientationRight: 
     rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0); 
     break; 
    case UIImageOrientationDown: 
     rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height); 
     break; 
    default: 
     rectTransform = CGAffineTransformIdentity; 
}; 
rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale); 

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectApplyAffineTransform(rect, rectTransform)); 
UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; 
CGImageRelease(imageRef); 

return result; 

}

+0

我得到了imageview的範圍縮放時改變部分。只是冷靜地想出如何調整權利。 所以我試過你的解決方案,這裏的交易它不會削減正確的矩形,它不考慮滾動視圖偏移量,這裏是例子 這裏是2個不同的可見部分 [1](http:// imgur的.com/tJw6m8W) [2](http://imgur.com/BCFPHGS) 在這兩種情況下我得到這個 [結果](http://imgur.com/6hAW4CW) ,這裏是相同的 [ 1](http://imgur.com/BAV2t1y) [2](http://imgur.com/gta91ZR) 結果 [結果](http://imgur.com/BQToolS) 另外我不'瞭解「類型」變量的用途。 –