2012-05-02 10 views
0

私は640x480ピクセルのイメージを持っています。私はそれを596x596ピクセルのUIImageにトリミングして配置する必要があります。空のスペースは黒色でなければなりません(画像の上下は黒色でなければなりません)。今、私は iOSのCGContextで黒い空白を表示するには

...このよう

-(UIImage*)cropImage:(UIImage *)theImage toFitSize:(CGSize)theSize 
{ 
CGFloat CROP_X = floorf((theImage.size.width-theSize.width)/2); 
CGFloat CROP_Y = floorf((theImage.size.height-theSize.height)/2); 

CGRect imageRect = CGRectMake(CROP_X, CROP_Y, theSize.width, theSize.height); 

UIGraphicsBeginImageContext(imageRect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(context, 0, 0, 0, 1); 
CGRect drawRect = CGRectMake(-imageRect.origin.x, -imageRect.origin.y, theImage.size.width, theImage.size.height); 
CGContextClipToRect(context, CGRectMake(0, 0, imageRect.size.width, imageRect.size.height)); 
[theImage drawInRect:drawRect]; 
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return finalImage; 
} 
それをトリミングしていると私も

- (UIImage *)croppedImage:(CGRect)bounds 
{ 
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds); 
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 
return croppedImage; 
} 

を試みたが、何もない空間が透明として出てきます。私はこれをどのようにして行うのですか?

おかげ

答えて

0

あなたが本当に画像を変更する必要がありますか?切り抜いたイメージを表示するだけの場合は、UIImageViewのbackgroundColorプロパティを設定して、目的の効果を得ることができます。

CGRect largerRect = CGRectMake(/* larger rect */); 
CGRect smallerRect = CGRectMake(/* smaller rect */); 

UIImage *croppedImage = [self cropImage:largerImage toFitSize:smallerRect]; 

// make the view the original size 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:largerRect]; 
imageView.image = croppedImage; 

// center the cropped image and give it a loud background color 
imageView.contentMode = UIViewContentModeCenter; 
imageView.backgroundColor = [UIColor redColor]; 
+0

素晴らしいたいとサイズを変更です!私は、イメージを網膜のサイズとして保存するのでcontentModeをUIViewContentModeScaleAspectFitに変更しましたが、通常のサイズで表示します。ありがとう! –

0

これはあなたが唯一のresizeimage機能と画像を使用した画像の作業コードのサイズを変更しているあなたは、幅、高さ

-`-(UIImage *)resizeImage:(UIImage *)image{ 
float width = image.size.width; 
float height = image.size.height; 

float maxSide = 310; 

if (width >= height && width > maxSide) 
{ 
    width = maxSide; 
    height = (height*(width/image.size.width)); 

} 
else{ 
    if (height > maxSide) 
    { 
     height = maxSide; 
     width = (width * (height/image.size.height)); 
    } 
} 

if ((int)width % 2 != 0) 
{ 
    width-- ; 
} 
if ((int)height %2 !=0) 
{ 
    height-- ; 
} 

UIImage *newImage; 

if (width != image.size.width) 
    newImage = [self scaleImage:image ToSize:CGSizeMake(width,height)]; 
else 
    newImage = image; 
return newImage;}- (UIImage*)scaleImage:(UIImage*)image ToSize:(CGSize)targetSize{ 
if (image == nil) 
    return nil; 

UIImage *sourceImage = image; 
UIImage *newImage = nil;   
CGSize imageSize = sourceImage.size; 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 
CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 
CGFloat scaleFactor = 0.0; 
CGFloat scaledWidth = targetWidth; 
CGFloat scaledHeight = targetHeight; 
CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
{ 
    CGFloat widthFactor = targetWidth/width; 
    CGFloat heightFactor = targetHeight/height; 

    if (widthFactor > heightFactor) 
     scaleFactor = widthFactor; // scale to fit height 
    else 
     scaleFactor = heightFactor; // scale to fit width 
    scaledWidth = width * scaleFactor; 
    scaledHeight = height * scaleFactor; 

    // center the image 
    if (widthFactor > heightFactor) 
    { 
     thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    } 
    else 
     if (widthFactor < heightFactor) 
     { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
}  

UIGraphicsBeginImageContext(targetSize); // this will crop 

CGRect thumbnailRect = CGRectZero; 
thumbnailRect.origin = thumbnailPoint; 
thumbnailRect.size.width = scaledWidth; 
thumbnailRect.size.height = scaledHeight; 

[sourceImage drawInRect:thumbnailRect]; 

newImage = UIGraphicsGetImageFromCurrentImageContext(); 
if(newImage == nil) 
{ 

} 


UIGraphicsEndImageContext(); 
return newImage; 

}

`

関連する問題