2012-07-12 88 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