2013-12-10 127 views
1

我正在使用Facebook創建應用程序。如果我要上傳照片至Facebook意味着我得到了以下信息任何給定的想法解決如何以編程方式調整圖像像素大小

「提供的user_generated照片的OG動作必須在兩個維度上至少480像素」

+0

您不想僅僅使用其中一種技術來放大和成像,因爲這樣會使像素過度並且降解。 (如果FB認爲可以對圖像進行像素化處理,那麼他們自己就可以做到這一點)。問題是如何修復生成圖像的工作流程,以便生成兩個維度都至少爲480像素的工作流程。描述你如何生成你上傳到Facebook的圖片。 – Rob

+0

我從照片庫@ Rob – Mahe

+0

上傳圖片您應該向我們展示從代碼庫中選取照片的代碼。圖書館中的大多數照片將會超過480像素,但可能會有更小的照片(非視網膜設備上的屏幕快照,保存在應用中的照片等)。如果您通過屏幕快照在庫中手動創建了圖像(例如,通過'renderInContext'),那麼請確保使用'UIGraphicsBeginImageContextWithOptions'的縮放比例爲'0'。 – Rob

回答

0

必須提供更大的圖像,至少有480px的寬度和高度。

2

我使用類似下面的函數來獲取任意大小的圖像。 原圖應該比大你想要的(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; 
} 
0

你的形象顯然是小於480像素寬或高。問題是原始圖像太小,或者您檢索的圖像不正確。理論上可以調整圖像的大小以使其更大,但這會導致像素化,這可能是不合需要的。

你應該告訴我們你是如何檢索圖像。例如,當我要挑我的內容庫中的照片,我會用改編自Picking an Item from the Photo Library相機編程主題代碼爲iOS

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