2012-02-13 94 views
28

sẽ muốn biết ai cần lưu 2 hình ảnh vào 1 hình ảnh.Cách kết hợp/ghép 2 hình ảnh thành 1

một trong những bức ảnh được cho phép để di chuyển, xoay và zoom in/out ...

tôi đang làm điều này, nhưng nó về cơ bản nắm bắt tất cả những thứ trên màn hình bao gồm các nút của tôi ...

UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *savedImg = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

kiểm tra liên kết này: -http: //stackoverflow.com/questions/6690444/merging-two-uiimage-into-1-to -be-savedtolibrary – Leena

+3

chuỗi bạn đã liên kết ... không hữu ích lắm: | – Vlad

+0

kiểm tra http://stackoverflow.com/questions/9208951/ios-merging-two-images-of-different-size/9209044#9209044 –

Trả lời

11
  1. trao đổi một trang con để thêm hình ảnh.
  2. thêm tất cả hình ảnh của bạn vào chế độ xem đó thay vì chế độ xem chính.
  3. để các nút và các nội dung khác ở trên chế độ xem chính.
  4. chỉ hiển thị chế độ xem có hình ảnh trong ngữ cảnh bitmap thay vì chế độ xem chính như bạn đang làm ngay bây giờ.
+1

Cảm ơn tôi đoán đây là cách tốt nhất :) – Desmond

60

Bạn có thể tạo ngữ cảnh đồ họa và vẽ cả hai hình ảnh trong đó. Bạn sẽ nhận được kết quả hình ảnh từ cả hai hình ảnh nguồn của bạn được kết hợp.

- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage { 
    UIImage *image = nil; 

    CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height)); 
    if (UIGraphicsBeginImageContextWithOptions != NULL) { 
     UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]); 
    } else { 
     UIGraphicsBeginImageContext(newImageSize); 
    } 
    [firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2), 
             roundf((newImageSize.height-firstImage.size.height)/2))]; 
    [secondImage drawAtPoint:CGPointMake(roundf((newImageSize.width-secondImage.size.width)/2), 
             roundf((newImageSize.height-secondImage.size.height)/2))]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 
+0

Hi Vlad, Cảm ơn bạn đã viết mã. nó hoạt động, nhưng không lưu bất kỳ chuyển động hoặc xoay nào mà tôi đã chỉnh sửa. Ngoài ra nó lưu trong tỷ lệ khía cạnh của hình ảnh. Bất kì lời đề nghị nào ? – Desmond

+0

Hi Vlad, cảm ơn – Desmond

+0

vui vì nó hoạt động. Không chắc chắn mặc dù những gì bạn đang sau? lúc bạn chỉnh sửa hình ảnh trong thời gian chạy? hoặc chỉ phóng to, di chuyển và xoay? – Vlad

9

Bạn có thể sử dụng phương pháp này, rất năng động và bạn có thể chỉ định vị trí bắt đầu của hình ảnh thứ hai và tổng kích thước của hình ảnh.

-(UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect withImageWidth:(int) width  
{ 

    CGSize size = CGSizeMake(width,40); 
    UIGraphicsBeginImageContext(size); 

    CGPoint pointImg1 = CGPointMake(0,0); 
    [img drawAtPoint:pointImg1]; 

    CGPoint pointImg2 = cropRect.origin; 
    [img2 drawAtPoint: pointImg2]; 

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return result; 
} 
37

// Objective-C

UIImage *image1 = [UIImage imageNamed:@"image1.png"]; 
UIImage *image2 = [UIImage imageNamed:@"image2.png"]; 

CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height); 

UIGraphicsBeginImageContext(size); 

[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)]; 
[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)]; 

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

//set finalImage to IBOulet UIImageView 
imageView.image = finalImage; 

// Swift

let topImage = UIImage(named: "image1.png") 
let bottomImage = UIImage(named: "image2.png") 

let size = CGSizeMake(topImage!.size.width, topImage!.size.height + bottomImage!.size.height) 
UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 

[topImage!.drawInRect(CGRectMake(0,0,size.width, topImage!.size.height))]; 
[bottomImage!.drawInRect(CGRectMake(0,topImage!.size.height,size.width, bottomImage!.size.height))]; 

let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

//set finalImage to IBOulet UIImageView 
mergeImage.image = newImage 
2

Dưới đây là một phương pháp để mở rộng UIImage để kết hợp nhiều hình ảnh:

class func combine(images: UIImage...) -> UIImage { 
    var contextSize = CGSizeZero 

    for image in images { 
     contextSize.width = max(contextSize.width, image.size.width) 
     contextSize.height = max(contextSize.height, image.size.height) 
    } 

    UIGraphicsBeginImageContextWithOptions(contextSize, false, UIScreen.mainScreen().scale) 

    for image in images { 
     let originX = (contextSize.width - image.size.width)/2 
     let originY = (contextSize.height - image.size.height)/2 

     image.drawInRect(CGRectMake(originX, originY, image.size.width, image.size.height)) 
    } 

    let combinedImage = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

    return combinedImage 
} 

Ví dụ:

UIImage.combine(image1, image2) 
7

Swift 3

Trong ví dụ này frontImage đang được rút ra bên trong hình ảnh khác sử dụng một insetBy 20% lợi nhuận.

Hình nền phải được vẽ trước và sau đó là hình ảnh phía trước theo thứ tự.

Tôi sử dụng này để đặt một hình ảnh "Play" biểu tượng ở phía trước một hình ảnh khung hình bên trong một UIImageView như dưới đây:

enter image description here

Cách sử dụng:

self.image = self.mergedImageWith(frontImage: UIImage.init(named: "play.png"), backgroundImage: UIImage.init(named: "backgroundImage.png"))) 

Phương pháp:

func mergedImageWith(frontImage:UIImage?, backgroundImage: UIImage?) -> UIImage{ 

    if (backgroundImage == nil) { 
     return frontImage! 
    } 

    let size = self.frame.size 
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 

    backgroundImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height)) 
    frontImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height).insetBy(dx: size.width * 0.2, dy: size.height * 0.2)) 

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 

    return newImage 
} 
1

Swift-3 (IOS10.3)

extension UIImage { 
    func combineWith(image: UIImage) -> UIImage { 
     let size = CGSize(width: self.size.width, height: self.size.height + image.size.height) 
     UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 

     self.draw(in: CGRect(x:0 , y: 0, width: size.width, height: self.size.height)) 
     image.draw(in: CGRect(x: 0, y: self.size.height, width: size.width, height: image.size.height)) 

     let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     return newImage 
    } 
} 

Cách sử dụng

let image1 = UIImage(named: "image1.jpg") 
let image2 = UIImage(named: "image2.jpg") 

yourImageView.image = image1?.combineWith(image: image2) 
Các vấn đề liên quan