2010-05-29 20 views
9

Ho preso uno sguardo a questa domanda: UIImage Shadow TroubleCrea nuovo UIImage con l'aggiunta di ombra per UIImage esistente

Ma la risposta accettata non ha funzionato per me.

Quello che sto cercando di fare è prendere una UIImage e aggiungere un'ombra ad essa, quindi restituire un UIImage intero, ombra e tutto.

Questo è quello che sto cercando:

- (UIImage*)imageWithShadow { 

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width, self.size.height + 1, CGImageGetBitsPerComponent(self.CGImage), 0, 
               colourSpace, kCGImageAlphaPremultipliedLast); 
    CGColorSpaceRelease(colourSpace); 

    CGContextSetShadow(shadowContext, CGSizeMake(0, -1), 1); 
    CGContextDrawImage(shadowContext, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage); 

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext); 
    CGContextRelease(shadowContext); 

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage]; 
    CGImageRelease(shadowedCGImage); 

    return shadowedImage; 
} 

Il risultato è che ho esattamente la stessa immagine di prima ho messo attraverso questo metodo.

Sto facendo questo nel modo corretto, o c'è qualcosa di ovvio che mi manca?

risposta

14

Ci sono diversi problemi con il tuo codice:

  • immagine il bersaglio è troppo piccolo. Assicurati che ci sia abbastanza spazio per disegnare l'ombra.
  • Considerare l'utilizzo di CGContextSetShadowWithColor per definire sia l'ombra che il suo colore.
  • Non dimenticare che il sistema di coordinate è capovolto, quindi l'origine è in basso a sinistra, non in alto a sinistra.

Fissando questi problemi, è necessario disegnare l'ombra.

- (UIImage*)imageWithShadow { 
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width + 10, self.size.height + 10, CGImageGetBitsPerComponent(self.CGImage), 0, 
              colourSpace, kCGImageAlphaPremultipliedLast); 
    CGColorSpaceRelease(colourSpace); 

    CGContextSetShadowWithColor(shadowContext, CGSizeMake(5, -5), 5, [UIColor blackColor].CGColor); 
    CGContextDrawImage(shadowContext, CGRectMake(0, 10, self.size.width, self.size.height), self.CGImage); 

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext); 
    CGContextRelease(shadowContext); 

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage]; 
    CGImageRelease(shadowedCGImage); 

    return shadowedImage; 
} 
+0

Perfetto! In realtà stavo aggiungendo 1 all'altezza per disegnare l'ombra, ma l'altra roba (l'impostazione dell'ombra, il sistema di coordinate capovolto) era azzeccata. –

+0

Dove dovrebbe essere implementato questo codice e come lo chiamereste per aggiungere un'ombra a un'immagine? –

+0

Il metodo presuppone che la classe possa fornire un CGImage e le sue dimensioni. Puoi invece modificare il metodo per prendere i parametri (CGImage e CGSize). –

5

Avevo bisogno di un metodo che potesse accettare un UIImmagine come parametro e restituire una UII più grande con ombre. I post qui mi sono stati di grande aiuto quindi ecco il mio metodo. L'immagine restituita ha un'ombra 5px centrata su ciascun lato.

+(UIImage*)imageWithShadowForImage:(UIImage *)initialImage { 

CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef shadowContext = CGBitmapContextCreate(NULL, initialImage.size.width + 10, initialImage.size.height + 10, CGImageGetBitsPerComponent(initialImage.CGImage), 0, colourSpace, kCGImageAlphaPremultipliedLast); 
CGColorSpaceRelease(colourSpace); 

CGContextSetShadowWithColor(shadowContext, CGSizeMake(0,0), 5, [UIColor blackColor].CGColor); 
CGContextDrawImage(shadowContext, CGRectMake(5, 5, initialImage.size.width, initialImage.size.height), initialImage.CGImage); 

CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext); 
CGContextRelease(shadowContext); 

UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage]; 
CGImageRelease(shadowedCGImage); 

return shadowedImage; 
} 
3

Avevo bisogno di un'ombra più grande per i miei UIImages. Ecco la mia variante della risposta che consente una dimensione shadow personalizzata senza offset.

- (UIImage*)imageWithShadow:(UIImage*)originalImage BlurSize:(float)blurSize { 

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, originalImage.size.width + (blurSize*2), originalImage.size.height + (blurSize*2), CGImageGetBitsPerComponent(originalImage.CGImage), 0, colourSpace, kCGImageAlphaPremultipliedLast); 
    CGColorSpaceRelease(colourSpace); 

    CGContextSetShadowWithColor(shadowContext, CGSizeMake(0, 0), blurSize, [UIColor blackColor].CGColor); 
    CGContextDrawImage(shadowContext, CGRectMake(blurSize, blurSize, originalImage.size.width, originalImage.size.height), originalImage.CGImage); 

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext); 
    CGContextRelease(shadowContext); 

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage]; 
    CGImageRelease(shadowedCGImage); 

    return shadowedImage; 
} 

utilizzarlo come:

UIImage *shadowedImage = [self imageWithShadow:myImage BlurSize:30.0f]; 
0

creare un'immagine di un CGImage come in questo caso, provare a

self.shadowImage.CGImage 
4

iOS 8/versione Swift (fatto come un'estensione UIImage):

extension UIImage { 

    func addShadow(blurSize: CGFloat = 6.0) -> UIImage { 

     let shadowColor = UIColor(white:0.0, alpha:0.8).cgColor 

     let context = CGContext(data: nil, 
           width: Int(self.size.width + blurSize), 
           height: Int(self.size.height + blurSize), 
           bitsPerComponent: self.cgImage!.bitsPerComponent, 
           bytesPerRow: 0, 
           space: CGColorSpaceCreateDeviceRGB(), 
           bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)! 

     context.setShadow(offset: CGSize(width: blurSize/2,height: -blurSize/2), 
          blur: blurSize, 
          color: shadowColor) 
     context.draw(self.cgImage!, 
        in: CGRect(x: 0, y: blurSize, width: self.size.width, height: self.size.height), 
        byTiling:false) 

     return UIImage(cgImage: context.makeImage()!) 
    } 
} 

(con convertita da & risposte capikaw di Laurent Etiemble)

EDIT 2016/10/31: convertito in Swift 3, e rimosso tutte le cose inutili

Usage:

let sourceImage: UIImage(named: "some image") 
let shadowImage = sourceImage.addShadow() 
+0

Per favore aggiornalo per Swift 2.0 – itsji10dra

+0

cool answer +1 per te – Vats

0

risposta di GK100 a Swift 2/iOS9

func addShadow(blurSize: CGFloat = 6.0) -> UIImage { 

    let data : UnsafeMutablePointer<Void> = nil 
    let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()! 
    let bitmapInfo : CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue) 

    let myColorValues:[CGFloat] = [0.0, 0.0, 0.0, 0.8] 
    let myColor = CGColorCreate(colorSpace, myColorValues) 

    let shadowContext : CGContextRef = CGBitmapContextCreate(data, Int(self.size.width + blurSize), Int(self.size.height + blurSize), CGImageGetBitsPerComponent(self.CGImage), 0, colorSpace, bitmapInfo.rawValue)! 

    CGContextSetShadowWithColor(shadowContext, CGSize(width: blurSize/2,height: -blurSize/2), blurSize, myColor) 
    CGContextDrawImage(shadowContext, CGRect(x: 0, y: blurSize, width: self.size.width, height: self.size.height), self.CGImage) 

    let shadowedCGImage : CGImageRef = CGBitmapContextCreateImage(shadowContext)! 
    let shadowedImage : UIImage = UIImage(CGImage: shadowedCGImage) 

    return shadowedImage 
} 
1

Il mio metodo che disegna un'ombra su un'immagine. Dispone di parametri ombra personalizzati e rende l'immagine in scala e dimensioni corrette. Dovrebbe essere implementato nell'estensione UIImage. (Swift 3).

func addingShadow(blur: CGFloat = 1, shadowColor: UIColor = UIColor(white: 0, alpha: 1), offset: CGSize = CGSize(width: 1, height: 1)) -> UIImage { 

    UIGraphicsBeginImageContextWithOptions(CGSize(width: size.width + 2 * blur, height: size.height + 2 * blur), false, 0) 
    let context = UIGraphicsGetCurrentContext()! 

    context.setShadow(offset: offset, blur: blur, color: shadowColor.cgColor) 
    draw(in: CGRect(x: blur - offset.width/2, y: blur - offset.height/2, width: size.width, height: size.height)) 
    let image = UIGraphicsGetImageFromCurrentImageContext()! 

    UIGraphicsEndImageContext() 

    return image 
}