Ios 如何以编程方式调整图像像素大小

Ios 如何以编程方式调整图像像素大小,ios,iphone,facebook,Ios,Iphone,Facebook,我正在使用facebook创建应用程序。若我试图将照片上传到facebook,意味着我收到了以下消息,请给出解决方法 为OG操作提供的用户生成的照片在两个维度上必须至少为480px您必须提供更大的图像,宽度和高度至少为480px。我使用follow这样的函数来获取任意大小的图像。 原始图像应该比你想要的大。附言:你可以尝试一个小图像 + (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)wantSize

我正在使用facebook创建应用程序。若我试图将照片上传到facebook,意味着我收到了以下消息,请给出解决方法


为OG操作提供的用户生成的照片在两个维度上必须至少为480px

您必须提供更大的图像,宽度和高度至少为480px。

我使用follow这样的函数来获取任意大小的图像。 原始图像应该比你想要的大。附言:你可以尝试一个小图像

+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)wantSize
{
    UIImage * targetImage;
    if (nil == image) {
        targetImage = nil;
    }else{
        CGSize size = image.size;
        CGRect rect;
        if (wantSize.width/wantSize.height > size.width/size.height) {
            rect.size.width = wantSize.height*size.width/size.height;
            rect.size.height = wantSize.height;
            rect.origin.x = (wantSize.width - rect.size.width)/2;
            rect.origin.y = 0;
        } else{
            rect.size.width = wantSize.width;
            rect.size.height = wantSize.width*size.height/size.width;
            rect.origin.x = 0;
            rect.origin.y = (wantSize.height - rect.size.height)/2;
        }
        UIGraphicsBeginImageContext(wantSize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        UIRectFill(CGRectMake(0, 0, wantSize.width, wantSize.height));//clear background
        [image drawInRect:rect];
        targetImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return targetImage;
}

您的图像明显小于480px宽或高。问题要么是原始图像太小,要么是检索不正确。从理论上讲,可以调整图像的大小使其更大,但这将导致可能不需要的像素化

您应该向我们展示如何检索图像。例如,当我想从我的库中挑选一张照片时,我将使用改编自iOS摄像头编程主题的代码:

然后,您显然必须实现didFinishPickingMediaWithInfo:


你不想仅仅使用其中一种技术来放大和成像,因为这将导致像素化并在过程中退化。如果FB认为可以对图像进行像素化,他们自己也会这样做。问题是如何修复生成图像的工作流,使其生成的图像在两个维度上都至少为480px。请描述您如何生成上载到Facebook的图像。我正在从照片上载图像library@RobYou应该向我们展示你从图书馆挑选照片的代码。你们库中的大多数照片都会超过480px,但也有可能是非视网膜设备上的小屏幕快照、应用程序保存的照片等。。如果从屏幕快照(例如通过renderInContext)在库中手动创建图像,请确保使用缩放为0的UIGraphicsBeginImageContextWithOptions。
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

// To instead show the controls to allow user to trim image, set this to YES;
// If no cropping available, set this to NO.

mediaUI.allowsEditing = YES;

mediaUI.delegate = delegate;
#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage, *editedImage, *imageToUse;

    // Handle a still image picked from a photo album
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {

        editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
        originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

        if (editedImage) {
            imageToUse = editedImage;
        } else {
            imageToUse = originalImage;
        }

        NSLog(@"image size = %@", NSStringFromCGSize(imageToUse.size));

        if (imageToUse.size.width < 480 || imageToUse.size.height < 480)
        {
                [[[UIAlertView alloc] initWithTitle:nil
                                            message:@"Please select image that is at least 480 x 480"
                                           delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil] show];
        }
        else
        {
            // do something with imageToUse
        }
    }

    [picker dismissViewControllerAnimated: YES completion:nil];
}