2012-06-14 21 views
14

Bu soru için üzgünüm, fakat burada S.O. ve hiçbir şey bulamadım.UIImage'da merkez karesi nasıl kırpılır?

Soru şudur: UIImage'da bir orta kareyi nasıl kırpılır?

Kod feryatını denedim ama başarılı olamadım. Kırpma işlemi gerçekleşir, ancak sol üst köşede.

-(UIImage*)imageCrop:(UIImage*)original 
{ 
    UIImage *ret = nil; 

    // This calculates the crop area. 

    float originalWidth = original.size.width; 
    float originalHeight = original.size.height; 

    float edge = fminf(originalWidth, originalHeight); 

    float posX = (originalWidth - edge)/2.0f; 
    float posY = (originalHeight - edge)/2.0f; 


    CGRect cropSquare = CGRectMake(posX, posY, 
            edge, edge); 


    // This performs the image cropping. 

    CGImageRef imageRef = CGImageCreateWithImageInRect([original CGImage], cropSquare); 

    ret = [UIImage imageWithCGImage:imageRef 
           scale:original.scale 
         orientation:original.imageOrientation]; 

    CGImageRelease(imageRef); 

    return ret; 
} 

cevap

15

Daha fazla bilgi ekleyerek, fotoğraf çekildikten sonra bu 'kırpma' kullanıyorum. Bazı testlerden sonra, çalışan bir şey buldum.

// If orientation indicates a change to portrait. 
if(original.imageOrientation == UIImageOrientationLeft || 
    original.imageOrientation == UIImageOrientationRight) 
{ 
    cropSquare = CGRectMake(posY, posX, 
          edge, edge); 

} 
else 
{ 
    cropSquare = CGRectMake(posX, posY, 
          edge, edge); 
}