2012-07-12 86 views
7

我已經在我的身邊做了很多工作。最後我需要幫助。感謝在ImageView裏面滾動縮放圖像scrollView

目標: 1)如何裝進滾動型

2)如何在裏面滾動視圖裁剪縮放圖像的ImageView。

我有一個imageView裏面的滾動視圖。我想要縮放後顯示在滾動視圖邊界內的裁剪圖像。我已經裁剪了圖像,但它不完全一樣,我想要的。

在這裏,我設置backgroundColor黑色到我的scrollView。而當我把imageView放在它裏面時,它不適合。 enter image description here

後滾動視圖內縮放圖像

enter image description here

後作物圖像

enter image description here

,我的代碼是在這裏:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CGRect frame1 = CGRectMake(50,50,200,200); 

    CGRect frame2 = CGRectMake(50,50,200,200); 

    imageView1=[[UIImageView alloc]initWithFrame:frame1]; 

    imageView1.image= [UIImage imageNamed:@"back.jpeg"]; 

    imageView1.backgroundColor=[UIColor brownColor]; 

    imageView1.contentMode = UIViewContentModeScaleAspectFit; 



    scroll=[[UIScrollView alloc]initWithFrame:frame2]; 

    scroll.backgroundColor=[UIColor blackColor]; 

    [scroll addSubview:imageView1]; 

    scroll.delegate=self; 

[self.view addSubview:scroll]; 

[self setContentSizeForScrollView]; 

self.imageView1.userInteractionEnabled = YES; 

} 

設定S1克羅爾視圖contentSize

-(void)setContentSizeForScrollView 
{ 
// scroll.contentSize = CGSizeMake(imageView1.frame.size.width,imageView1.frame.size.height); 

    scroll.contentSize = CGSizeMake(200, 200); 
    scroll.minimumZoomScale = .50; 
    scroll.maximumZoomScale = 1.5; 
} 

和我的作物邏輯是

-(IBAction)cropButtonClicked 
{ 
    //Calculate the required area from the scrollview 

CGRect rect = CGRectMake(50, 50, 200,200); 


UIImage *image = [self imageByCropping:imageView1.image toRect:rect]; 

imageView1.image=image; 


imageView1.contentMode = UIViewContentModeScaleAspectFit; 


} 

而且這種方法的裁剪圖像:

- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{ 
    CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea); 
    UIImage* cropped = [UIImage imageWithCGImage:cropImageRef]; 

    CGImageRelease(cropImageRef); 
    return cropped; 
} 

回答

3

我自己的問題的答案:經過多方努力我找到了答案我的兩個問題。

它對我很好。我在這裏分享,可能會幫助別人。 :)

1)適合圖像查看滾動視圖內。我使用link

- (void)centerScrollViewContents { 

CGSize boundsSize = scroll.bounds.size; 
CGRect contentsFrame = self.imageView1.frame; 

if (contentsFrame.size.width < boundsSize.width) { 
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f; 
} 
else { 
contentsFrame.origin.x = 0.0f; 
} 

if (contentsFrame.size.height < boundsSize.height) { 
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f; 
} 
else { 
contentsFrame.origin.y = 0.0f; 
} 

self.imageView1.frame = contentsFrame; 
} 

2)在內部滾動視圖中裁剪縮放圖像。我用這個link

UIGraphicsBeginImageContext(CGSizeMake(200, 200)); 

    [scroll.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    imageView1.contentMode = UIViewContentModeScaleAspectFill; 

    UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil); 

    return fullScreenshot; 
2

我有一個類似的問題最近,我得到了使用相對想法溶液(XA,B = XA,C - XB,C)

-(IBAction)crop:(id)sender{ 

     // F = frame 
     // i = image 
     // iv = imageview 
     // sv = self.view 
     // of = offset 

     //Frame Image in imageView coordinates 
     CGRect Fi_iv = [self frameForImage:self.image inImageViewAspectFit:self.imageView]; 

     //Frame ImageView in self.view coordinates 
     CGRect Fiv_sv = self.imageView.frame; 

     //Frame Image in self.view coordinates 
     CGRect Fi_sv = CGRectMake(Fi_iv.origin.x + Fiv_sv.origin.x 
            ,Fi_iv.origin.y + Fiv_sv.origin.y, 
            Fi_iv.size.width, Fi_iv.size.height); 
     //ScrollView offset 
     CGPoint offset = self.scrollView.contentOffset; 

     //Frame Image in offset coordinates 
     CGRect Fi_of = CGRectMake(Fi_sv.origin.x - offset.x, 
             Fi_sv.origin.y - offset.y, 
             Fi_sv.size.width, 
             Fi_sv.size.height); 

     CGFloat scale = self.imageView.image.size.width/Fi_of.size.width; 

     //the crop frame in image offset coordinates 
     CGRect Fcrop_iof = CGRectMake((self.cropView.frame.origin.x - Fi_of.origin.x)*scale, 
             (self.cropView.frame.origin.y - Fi_of.origin.y)*scale, 
             self.cropView.frame.size.width*scale, 
             self.cropView.frame.size.height*scale); 

     UIImage *image = [self image:self.imageView.image cropRect:Fcrop_iof]; 
     // Use this crop image... 

您可以剪裁使用圖像:

//Use this code to crop when you have the right frame 
-(UIImage*)image:(UIImage *)image cropRect:(CGRect)frame 
{ 
    // Create a new UIImage 
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, frame); 
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return croppedImage; 
} 

由於您使用的看點飛度ImageView的,你需要計算的ImageView

內的圖像幀
-(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView 
{ 
    float imageRatio = image.size.width/image.size.height; 

    float viewRatio = imageView.frame.size.width/imageView.frame.size.height; 

    if(imageRatio < viewRatio) 
    { 
     float scale = imageView.frame.size.height/image.size.height; 

     float width = scale * image.size.width; 

     float topLeftX = (imageView.frame.size.width - width) * 0.5; 

     return CGRectMake(topLeftX, 0, width, imageView.frame.size.height); 
    } 
    else 
    { 
     float scale = imageView.frame.size.width/image.size.width; 

     float height = scale * image.size.height; 

     float topLeftY = (imageView.frame.size.height - height) * 0.5; 

     return CGRectMake(0, topLeftY, imageView.frame.size.width, height); 
    } 
} 

我希望這個代碼可以爲你工作,因爲它爲我工作。

最好,

拉斐爾。

+0

我在搖這個。我在上面的代碼中發現的唯一問題是在標題「//圖像偏移座標中的裁剪框架」下方的代碼中。你正在使用self.cropview.frame,而我使用self.cropview.bounds。 – 2016-01-27 05:43:13

+1

酷.....謝謝老兄..工作真棒.. – Gokul 2016-09-23 15:26:15