2011-07-11 3 views

Répondre

36

Vous pouvez ajouter un sous-vue à votre UIImageView contenant une autre image avec le petit triangle rassasiés. Ou vous pouvez dessiner à l'intérieur de la première image:

CGFloat width, height; 
UIImage *inputImage; // input image to be composited over new image as example 

// create a new bitmap image context at the device resolution (retina/non-retina) 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0);   

// get context 
CGContextRef context = UIGraphicsGetCurrentContext();  

// push context to make it current 
// (need to do this manually because we are not drawing in a UIView) 
UIGraphicsPushContext(context);        

// drawing code comes here- look at CGContext reference 
// for available operations 
// this example draws the inputImage into the context 
[inputImage drawInRect:CGRectMake(0, 0, width, height)]; 

// pop context 
UIGraphicsPopContext();        

// get a UIImage from the image context- enjoy!!! 
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 

// clean up drawing environment 
UIGraphicsEndImageContext(); 

Ce code (source here) va créer une nouvelle UIImage que vous pouvez utiliser pour initialiser un UIImageView.

20

Vous pouvez essayer cela, fonctionne parfaitement pour moi, sa catégorie UIImage:

- (UIImage *)drawImage:(UIImage *)inputImage inRect:(CGRect)frame { 
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0); 
    [self drawInRect:CGRectMake(0.0, 0.0, self.size.width, self.size.height)]; 
    [inputImage drawInRect:frame]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

ou Swift:

extension UIImage { 
    func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! { 
     UIGraphicsBeginImageContext(size) 
     draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
     image.draw(in: rect) 
     let result = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return result 
    } 
} 
+0

Merci mec, c'est un extrait très utile. –

+1

Cela fonctionne bien, merci. Je suggère que vous utilisiez 'UIGraphicsBeginImageContextWithOptions (size, false, 0)', cependant. Cela vous donnera une image avec la bonne résolution pour l'écran. (La valeur par défaut ne produira qu'une image x1, qui sera presque certainement floue.) – Womble

Questions connexes