2012-08-30 60 views
0

我製作了一款相機應用程序,在該應用程序中,我點擊一張圖像並將其剪切成一半,然後單擊其他圖像並從中剪下另一半。
然後這兩個圖像合併我已成功完成此任務,但問題是合併不順利。如何在ios應用程序中合併兩個半圖像?

兩半之間存在明顯的分離......我想知道如何平滑它?

//------merging the two images------ 
- (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second 
{ 
   // get size of the first image 
   CGImageRef firstImageRef = first.CGImage; 
   CGFloat firstWidth = CGImageGetWidth(firstImageRef); 
   CGFloat firstHeight = CGImageGetHeight(firstImageRef); 
    
   // get size of the second image 
   CGImageRef secondImageRef = second.CGImage; 
   CGFloat secondWidth = CGImageGetWidth(secondImageRef); 
   CGFloat secondHeight = CGImageGetHeight(secondImageRef); 
    
   // build merged size 
   CGSize mergedSize = CGSizeMake((firstWidth+secondWidth), MAX(firstHeight, secondHeight)); 
    
   // capture image context ref 
   UIGraphicsBeginImageContext(mergedSize); 
    
   //Draw images onto the context 
   [first drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)]; 
   //[second drawInRect:CGRectMake(firstWidth, 0, secondWidth, secondHeight)]; 
   [second drawInRect:CGRectMake(firstWidth, 0, secondWidth, secondHeight) blendMode:kCGBlendModeNormal alpha:1.0]; 
    
   // assign context to new UIImage 
   UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    
   // end context 
   UIGraphicsEndImageContext(); 
    
   return newImage; 
  
} 

感謝

(我接受downvote是健康的批評,但請GV RSN,這樣我可以改進)

+0

纔有可能真正合並這兩個圖像的二進制數據,所以你只需要畫一個像? – Fab1n

+0

兩幅圖像的縱橫比都一樣嗎?用戶是否可以像調整方向一樣調整它? – Pochi

+0

@luis奧斯卡耶可以做出這樣的假設 – amar

回答

4

如果我不是錯了,那麼你想這樣。

UIImageView *imgView1, *imgView2, *imgView3; 
imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1"]]; 
imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2"]]; 
imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image3"]]; 
imgView1.frame = CGRectMake(0, 0, 50, 50); 
imgView2.frame = CGRectMake(50, 50, 100, 100); 
imgView3.frame = CGRectMake(100, 100, 200, 200); 
[referenceView addSubview:imgView1]; 
[referenceView addSubview:imgView2]; 
[referenceView addSubview:imgView3]; 
UIGraphicsBeginImageContext(referenceView.bounds.size); 
[referenceView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
resultView = [[UIImageView alloc] initWithImage:finalImage]; 
resultView.frame = CGRectMake(0, 0, 320, 480); 
[self.view addSubview:resultView]; 
referenceView.hidden = YES; 
0
UIImageView *referenceView = [[UIImageView alloc]init]; 
referenceView.frame = CGRectMake(0, 0, 1024, 768); 
referenceView.hidden = NO; 

[self.view insertSubview:referenceView belowSubview:selfView]; 

UIImageView *imgView1, *imgView2,*imgView3; 
imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1"]]; 
imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2"]]; 
imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image3"]]; 


imgView1.frame = CGRectMake(0, 0, 50, 50); 
imgView2.frame = CGRectMake(50, 50, 100, 100); 
imgView3.frame = CGRectMake(100, 100, 200, 200); 

[referenceView addSubview:imgView1]; 
[referenceView addSubview:imgView2]; 
[referenceView addSubview:imgView3]; 
UIImage *finalImage = nil; 
UIGraphicsBeginImageContext(referenceView.bounds.size); 
[referenceView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return finalImage; 
相關問題