2016-11-08 11 views
-1

ich neuen CGContext mit erstellt:CGContextRef zieht unscharf oder unscharfe Bilder

 NSUInteger width = newRect.size.width; 
     NSUInteger height = newRect.size.height; 

     NSUInteger bytesPerPixel = 4; 
     NSUInteger bytesPerRow = bytesPerPixel * width; 
     NSUInteger bitsPerComponent = 8; 

     UInt32 * pixels; 
     pixels = (UInt32 *) calloc(height * width, sizeof(UInt32)); 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

     CGContextRef context = CGBitmapContextCreate(pixels, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); 

     CGContextSetShouldAntialias(self.graphContext, false); 
     CGContextSetInterpolationQuality(self.graphContext, kCGInterpolationHigh); 

Als ich mir seltsam Fuzzy-Objekten wie dies versuche, Kreis oder Text mit diesem Kontext zu ziehen:

enter image description here

enter image description here

Wenn ich CGContextSetShouldAntialias auf True Bilder setzt, wird zu blured:

enter image description here

enter image description here

Ich mag Texte und Bilder werden wie üblich Etikett oder Ansichten (nicht verschwommen, aber nicht unscharf). Ich kann nur diesen Ansatz verwenden, um CGContextRef zu erhalten.

Mein Code für Text Zeichnung:

CGMutablePathRef path = CGPathCreateMutable(); 
CGPathAddRect(path, NULL, rect); 

NSAttributedString* attString = [[NSAttributedString alloc] 
            initWithString:text 
            attributes:self.attributes]; 

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); 

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, 
         CFRangeMake(0, [attString length]), path, NULL); 

CTFrameDraw(frame, self.graphContext); 

CFRelease(frame); 
CFRelease(path); 
CFRelease(framesetter); 

-Code für das Zeichnen von Kreisen:

CGRect minPath = CGRectMake(xPoint - extremaPointWidth/2, minY - extremaPointWidth /2, extremaPointWidth, extremaPointWidth); 

    CGContextAddPath(self.graphContext, createRoundedCornerPath(minPath, cornerRadius)); 
    CGContextFillPath(self.graphContext); 

Erstellen von Rundungs ​​Pfad

// create a mutable path 
CGMutablePathRef path = CGPathCreateMutable(); 

// get the 4 corners of the rect 
CGPoint topLeft = CGPointMake(rect.origin.x, rect.origin.y); 
CGPoint topRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y); 
CGPoint bottomRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); 
CGPoint bottomLeft = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height); 

CGPathMoveToPoint(path, NULL, topLeft.x + cornerRadius, topLeft.y); 

CGPathAddLineToPoint(path, NULL, topRight.x - cornerRadius, topRight.y); 

CGPathAddQuadCurveToPoint(path, NULL, topRight.x, topRight.y, topRight.x, topRight.y + cornerRadius); 

CGPathAddLineToPoint(path, NULL, bottomRight.x, bottomRight.y - cornerRadius); 

CGPathAddQuadCurveToPoint(path, NULL, bottomRight.x, bottomRight.y, bottomRight.x - cornerRadius, bottomRight.y); 

CGPathAddLineToPoint(path, NULL, bottomLeft.x + cornerRadius, bottomLeft.y); 

CGPathAddQuadCurveToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomLeft.x, bottomLeft.y - cornerRadius); 

CGPathAddLineToPoint(path, NULL, topLeft.x, topLeft.y + cornerRadius); 

CGPathAddQuadCurveToPoint(path, NULL, topLeft.x, topLeft.y, topLeft.x + cornerRadius, topLeft.y); 

return path; 

Dank.

UPDATE: Nein Wenn ich nur gerade Linie oder Rechteck es ist in Ordnung

Antwort

0

ich seltsam Fuzzy-Objekte wie diese

bekommen, haben Sie nicht. Sie erhalten eine Bitmap. Sie müssen etwas tun sonst, um ein Objekt zu erhalten, das Sie sehen können - und Sie haben nicht gezeigt, was es ist, dass Sie tun. Aber ist das nicht das ganze Problem? Sie müssen ein Bitmap in ein UIImage verwandeln, und das dauert Code. Wenn Sie es in einer blockartigen, pixelierten Weise tun, erhalten Sie ein blockiertes, pixeliertes Ergebnis.

verwendete ich Ihre Bitmap Kontext Code und zog in sie einen gefüllten Kreis, und wandte sich dann die Bitmap-Kontext in eine UIImage und setzen die UIImage in einem UIImageView, und es sieht wie folgt aus:

enter image description here

Nun, , dass scheint glatt genug. Es ist also sicherlich nicht Ihr Bitmap-Kontext, sondern das Problem, wie Sie von dort zu etwas kommen sichtbar das ist das Problem.

Allerdings persönlich verstehe ich nicht, warum Sie all diese Probleme gehen. Wenn Sie ein Bild erstellen möchten, das Sie in der Benutzeroberfläche anzeigen möchten, verwenden Sie einfach UIGraphicsBeginImageContextWithOptions wie alle anderen auch?

+0

Danke für Ihre Antwort. Also, vielleicht ist die richtige Frage, wie man das richtige Bild erhält? Ich benutze diese Zeilen: CGImageRef cgImage = CGBitmapContextCreateImage (self.graphContext); UIImage * point = [UIImage imageWithCGImage: cgImage]; – Artem

+0

Richtig, du verwendest also einen Skalen = 1 Grafikkontext in einer skala = 2 Welt. Wenn Sie, wie ich in meiner Antwort vorgeschlagen habe, 'UIGraphicsBeginImageContextWithOptions' mit der Skalierung 0 verwenden, wäre das ganze Problem für Sie erledigt. – matt

+0

Aber wie kann ich dieses Problem in meinem Fall beheben? Ich habe wirklich versucht, Ihren Vorschlag zu verwenden, bevor ich mit Bitmap auftrat und nur Bitmap brauche. In meinem Fall muss ich Tausende Kreise zeichnen und UIGraphicsBeginImageContextWithOptions machen mir einige Probleme – Artem