2012-08-08 38 views
20

Tôi hiện đang phát triển một photoshop đơn giản như ứng dụng trên iphone. Khi tôi muốn làm phẳng các lớp của mình, các nhãn ở vị trí tốt nhưng có kích thước phông chữ kém. Đây là mã của tôi để làm phẳng:Cách tạo hình ảnh từ UILabel?

UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument)); 

for (UILabel *label in arrayLabel) { 
    [label drawTextInRect:label.frame]; 
} 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Bất kỳ ai cũng có thể giúp tôi?

Trả lời

38

Từ: pulling an UIImage from a UITextView or UILabel gives white image.

// iOS

- (UIImage *)grabImage { 
    // Create a "canvas" (image context) to draw in. 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0); // high res 
    // Make the CALayer to draw in our "canvas". 
    [[self layer] renderInContext: UIGraphicsGetCurrentContext()]; 

    // Fetch an UIImage of our "canvas". 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    // Stop the "canvas" from accepting any input. 
    UIGraphicsEndImageContext(); 

    // Return the image. 
    return image; 
} 

// Swift mở rộng w/sử dụng. tín dụng @Heberti Almeida ở dưới bình luận của

extension UIImage { 
    class func imageWithLabel(label: UILabel) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0) 
     label.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     let img = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return img 
    } 
} 

Việc sử dụng:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22)) 
label.text = bulletString 
let image = UIImage.imageWithLabel(label) 
+0

Có, tôi đã xem bài đăng này nhưng với giải pháp này, tôi không thể tải tệp có độ phân giải cao. Nhưng bạn làm cho tôi hiểu đâu là vấn đề của tôi! Cảm ơn bạn rất nhiều !! – Jonathan

+6

@DJPlayer Để có được một hình ảnh sắc nét cho màn hình võng mạc, chúng ta cần phải thay thế dòng đầu tiên trong phương thức của bạn bằng dòng này: 'UIGraphicsBeginImageContextWithOptions (self.bounds.size, self.opaque, 0.0);' như được thấy trong [câu hỏi này] (http://stackoverflow.com/q/4334233/2471006) và câu trả lời được chấp nhận của nó. Có lẽ bạn có thể muốn cập nhật câu trả lời của bạn bao gồm thông tin đó. – anneblue

+0

@anneblue cảm ơn! – nemesis

5

bạn cần phải làm cho nhãn trong một bối cảnh

thay

[label drawTextInRect:label.frame]; 

với

[label.layer renderInContext:UIGraphicsGetCurrentContext()]; 

và bạn sẽ cần phải tạo một hình ảnh cho mỗi nhãn, do đó hãy di chuyển vòng lặp cho đến bên ngoài ngữ cảnh bắt đầu và kết thúc cuộc gọi

1

Bạn có thể sử dụng renderInContext trên bất kỳ CALayer nào. Nó sẽ vẽ lớp của chế độ xem của bạn trên ngữ cảnh. Vì vậy, bạn có thể thay đổi nó thành một hình ảnh. Ngoài ra nếu bạn thực hiện renderInContext trên một khung nhìn, tất cả các bản xem con của nó sẽ được vẽ lên ngữ cảnh. Vì vậy, thay vì lặp qua tất cả các nhãn, trong khi thêm các nhãn, bạn có thể thêm các nhãn đó vào một containerView. Và chỉ cần thực hiện renderInContext trên containerView đó.

12

Tôi đã tạo ra một phần mở rộng Swift để render UILabel thành một UIImage:

extension UIImage { 
    class func imageWithLabel(label: UILabel) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0) 
     label.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     let img = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return img 
    } 
} 

EDIT: Cải thiện Swift 4 phiên bản

extension UIImage { 
    class func imageWithLabel(_ label: UILabel) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0) 
     defer { UIGraphicsEndImageContext() } 
     label.layer.render(in: UIGraphicsGetCurrentContext()!) 
     return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage() 
    } 
} 

Việc sử dụng rất đơn giản:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22)) 
label.text = bulletString 
let image = UIImage.imageWithLabel(label) 
+1

Bạn, thưa bạn của tôi là anh hùng đích thực! – rilar

+0

^^ Những gì anh ấy nói ^^ – bkwebhero

1

cho Swift , Tôi sử dụng tiện ích mở rộng UIView này:

// Updated for Swift 3.0 
extension UIView { 
    var grabbedImage:UIImage? { 
     get { 
      UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0) 
      layer.render(in: UIGraphicsGetCurrentContext()!) 
      let img = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 
      return img 
     } 
    } 
} 
Các vấn đề liên quan