문제

카메라에서 uiimages를 받고 표시 할 UiimageViews에 할당합니다. 이 작업을 수행하면 카메라는 1200 x 1600 픽셀 이미지를 제공하여 응용 프로그램에서 UIIMAGEVIEW에 할당합니다. 이미지는이 조건에서 이미지보기에 예상대로 표시됩니다. 그러나 검색 된 UIIMAGE를 UIIMAGEVIEW에 할당하기 전에 검색된 UIIMAGE를 크기를 조정하려고하면 이미지가 예상대로 크기 조정되고 있지만 어딘가에 (크기 조정 코드에서?)에 문제가 있습니다. 크기가 조정 된 UIIMAGE를 UIIMAGEVIEW에 할당하면 이미지가 90도 회전하고 종횡비 (1200 X 1600 픽셀)가 변하지 않은 것처럼 늘어나는 것처럼 보입니다 ...

카메라에서 uiimage를 얻기 위해 이것을 사용하고 있습니다.

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{

        myImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        myResizedImg = [self resizeImage:myImg width:400 height:533];
        [myImageView setImage:myResizedImg];

}

나는 이것을 크기를 조정하기 위해 이것을 사용하고 있습니다.

-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height
{

    CGImageRef imageRef = [anImage CGImage];

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;


    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);

    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;  
}

질문 : 픽셀을 회전시키지 않고 카메라에서 당기는 uiimage를 어떻게 크기를 조정합니까?

도움이 되었습니까?

해결책

코드가 작동하지 않는 이유는 귀하가 가진 코드의 이미지 방향이 고려되지 않기 때문입니다. 구체적으로 이미지 방향이 오른쪽/왼쪽 인 경우 이미지를 회전하고 너비/높이를 스왑해야합니다. 다음은이를 수행하는 몇 가지 코드입니다.

-(UIImage*)imageByScalingToSize:(CGSize)targetSize
{
    UIImage* sourceImage = self; 
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    }   

    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -targetWidth, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return newImage; 
}

이렇게하면 이미지 크기를 조정하고 올바른 방향으로 회전합니다. 라디안에 대한 정의가 필요한 경우 다음과 같습니다.

static inline double radians (double degrees) {return degrees * M_PI/180;}

다니엘이 준 대답도 정확하지만 uigraphicsbeginimagecontext ()를 사용하고 있기 때문에 스레드 안전이 아니라는 문제가 발생합니다. 위의 코드는 CG 함수 만 사용하므로 모두 설정됩니다. 또한 이미지를 크기를 조정하고 적절한 측면에 채우는 비슷한 기능을 가지고 있습니다. 그것이 당신이 찾고있는 것인지 알려주세요.

참고 : 원래 기능을 얻었습니다 이 게시물, 그리고 JPEG에서 작동하기 위해 약간의 수정을했습니다.

다른 팁

코드 중 일부 에도이 문제가 있었는데이 코드가 작동하는 코드를 찾았습니다. 확인하고 찾은 내용을 알려주세요.

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

스위프트 3

나는 이것을 추가한다 didFinishPickingMediaWithInfo 방법을 사용한 다음 사용합니다 image 회전에 대해 걱정하지 않고.

var image = info[UIImagePickerControllerOriginalImage] as! UIImage
if (image.imageOrientation != .up) {
  UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
  image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  image = UIGraphicsGetImageFromCurrentImageContext()!
  UIGraphicsEndImageContext()
}

양쪽의 너비 또는 높이로 자르면 직사각형 이미지 (수평 또는 수직)에서 정사각형 이미지를 만들려면 내 코드를 사용하십시오. 차이점은 스트레칭을하지는 않지만 자르고 있다는 것입니다. 수정으로 최상위 코드에서 만들었습니다.

//Cropping _image to fit it to the square by height or width

CGFloat a = _image.size.height;
CGFloat b = _image.size.width;
CGRect cropRect;

if (!(a==b)) {
    if (a<b) {
        cropRect = CGRectMake((b-a)/2.0, 0, a, a);
        b = a;
    }
    else if (b<a) {
        cropRect = CGRectMake(0, (a-b)/2.0, b, b);
        a = b;
    }

    CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect);

    UIImage* sourceImage = _image; 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
    CGContextRef bitmap;
    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
    }

    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -a);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -a, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, a, a);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    _image = [UIImage imageWithCGImage:ref];
    CGImageRelease(imageRef);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top