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

时间:2013-12-10 13:12:29

标签: ios iphone facebook

我正在使用facebook创建应用程序。如果我试图将照片上传到脸书Facebook意味着我得到了以下消息任何给出解决的想法

“提供的OG操作的user_generated照片在两个维度中必须至少为480px”

3 个答案:

答案 0 :(得分:2)

我使用像follow这样的函数来获取任何大小的图像。 原始图像应该比你想要的大。(ps:你可以尝试一点图像)

+ (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;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您的图像明显小于480px宽或高。问题是原始图像太小,或者您正在错误地检索它。理论上,您可以调整图像大小以使其更大,但这会导致像素化,这可能是不可取的。

您应该告诉我们您是如何检索图像的。例如,当我想从我的库中选择一张照片时,我将使用来自 iOS相机编程主题Picking an Item from the Photo Library改编的代码:

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;

然后,您显然必须实施didFinishPickingMediaWithInfo

#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];
}