2012-07-12 25 views
7

私はすでに私の側からたくさんの努力をしています。ついに私は助けが必要です。おかげスクロールビュー内のImageViewでズームした画像をクロップする

目標: 1)私は内部scrollViewでズーム画像をトリミング方法ScrollView

2)の内側にImageViewのに合わせてどのように。

私はスクロールビュー内にimageViewを持っています。スクロールビューの境界内に表示されているズーム後にクロップ画像が必要です。私はすでにイメージを切り取ったが、私が望むものと全く同じではなかった。

ここでは、backgroundColor Blackを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; 

} 

設定のcrollビューの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)内部scrollViewでズーム画像をトリミング使用しています。あなたはトリミングすることができます - (Xbを、C Xa因子、B = Xa因子、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... 

私はこのlink

UIGraphicsBeginImageContext(CGSizeMake(200, 200)); 

    [scroll.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *fullScreenshot = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    imageView1.contentMode = UIViewContentModeScaleAspectFill; 

    UIImageWriteToSavedPhotosAlbum(fullScreenshot, nil, nil, nil); 

    return fullScreenshot; 
2

を使用し、私は最近、同様の問題があった、私は相対的なアイデアを用いて溶液を得ました画像使用:

//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.boundsを使ったのに対し、self.cropview.frameを使っています。 –

+1

クール.....ありがとう。 – Gokul

関連する問題