2012-08-09 21 views
16

सब कुछ FBProfilePictureView के साथ ठीक काम कर रहा है, लेकिन मैं FBProfilePictureView से कि तस्वीर मिल और यह एक UIImage में बदल जाने की जरूरत है।मैं FBProfilePictureView को UIImage में कैसे परिवर्तित कर सकता हूं?

मुझे यह कैसे करना चाहिए?

मैं इस उपयोग करने की कोशिश:

UIGraphicsBeginImageContext(self.profilePictureView.frame.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
self.TestPictureOutlet.image = viewImage; 

लेकिन मेरे समाधान के लिए यह does not काम।

+0

सुनिश्चित नहीं हैं कि क्यों वे UIImage का पर्दाफाश नहीं किया। लंगड़ा समाधान, लेकिन क्या आप सिर्फ ग्राफ एपीआई का उपयोग कर सकते हैं और यूआरएल के माध्यम से लोड कर सकते हैं? –

उत्तर

17

FBProfilePictureView एक UIView है, इस UIView एक UIImageView होता है, कि आपकी छवि है, तो आप उस UIImageView से UIImage प्राप्त कर सकते हैं:

profilePictureView एक FBProfilePictureView है

UIImage *image = nil; 

for (NSObject *obj in [profilePictureView subviews]) { 
    if ([obj isMemberOfClass:[UIImageView class]]) { 
     UIImageView *objImg = (UIImageView *)obj; 
     image = objImg.image; 
     break; 
    } 
} 

संपादित करें: एक और जोड़ने रास्ता अधिक तेज़ी से करें लेकिन एक ही चीज करें

__block UIImage *image = nil; 

[self.view.subviews enumerateObjectsUsingBlock:^(NSObject *obj, NSUInteger idx, BOOL *stop) { 
    if ([obj isMemberOfClass:[UIImageView class]]) { 
     UIImageView *objImg = (UIImageView *)obj; 
     image = objImg.image; 
     *stop = YES; 
    } 
}]; 
+2

यह रिटर्न शून्य –

+2

यूप ... रिटर्न शून्य। क्या यह किसी के लिए काम कर रहा है? – Dev

+1

वापसी नहीं के बराबर है, तो छवि अभी तक डाउनलोड नहीं है, तो आप एक देरी सेट की जरूरत है या UIImage जब छवि डाउनलोड कर रहे हैं, नहीं तो वापसी नहीं के बराबर लोड है, लेकिन इस कोड – busta117

0

यहां समाधान।

कदम:

यकीन है कि तुम मेरे मामले में वर्ग "FBProfilePictureView" की वस्तु के लिए अमेरिकन प्लान उपयोगकर्ता आईडी निर्दिष्ट बनाओ इस वर्ग वस्तु "userPictureImageView"

-(void)saveFBUserImage 
{ 

    CGSize imageSize = self.userPictureImageView.frame.size; 

    UIGraphicsBeginImageContext(imageSize); 
    CGContextRef imageContext = UIGraphicsGetCurrentContext(); 

    [self.userPictureImageView.layer renderInContext: imageContext]; 
    UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    NSData *imageData = UIImageJPEGRepresentation(viewImage,1); 

    UIImage * img = [UIImage imageWithData:imageData]; 

    NSString *filePath = <specify your path here>; 

    CGSize size = img.size; 

    if(size.height > 50) 
     size.height = 50; 
    if(size.width > 50) 
     size.width = 50; 

    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); 
    CGSize size2 = rect.size; 

    UIGraphicsBeginImageContext(size2); 

    [img drawInRect:rect]; 
    img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    NSData *newImageData = UIImageJPEGRepresentation(img, 1.0); 
    [newImageData writeToFile:filePath atomically:YES]; 
} 

है यही कारण है यह। :-)

3

दोनों से ऊपर समाधान UIImage FBProfilePictureView से बाहर वस्तु प्राप्त करने के लिए ठीक से काम का उल्लेख किया। है कि आपको FBProfilePictureView से छवि प्राप्त करने से पहले कुछ देरी लगाने की जरूरत है, है। तरह:

[FBRequest requestForMe] startWithCompletionHandler: 
    ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { 
     if (!error) { 

      myNameLbl.text = user.name; 
      profileDP.profileID = user.id; 

//NOTE THIS LINE WHICH DOES THE MAGIC 

[self performSelector:@selector(getUserImageFromFBView) withObject:nil afterDelay:1.0]; 

}]; 

- (void)getUserImageFromFBView{ 

    UIImage *img = nil; 

    //1 - Solution to get UIImage obj 

    for (NSObject *obj in [profileDP subviews]) { 
     if ([obj isMemberOfClass:[UIImageView class]]) { 
      UIImageView *objImg = (UIImageView *)obj; 
      img = objImg.image; 
      break; 
     } 
    } 

    //2 - Solution to get UIImage obj 

// UIGraphicsBeginImageContext(profileDP.frame.size); 
// [profileDP.layer renderInContext:UIGraphicsGetCurrentContext()]; 
// img = UIGraphicsGetImageFromCurrentImageContext(); 
// UIGraphicsEndImageContext(); 

//Here I'm setting image and it works 100% for me. 

    testImgv.image = img; 

} 

सादर!

आमिर अली - आईओएस एप्लिकेशन डेवलपर

@Time समूह (TGD)

0

मैं समाधान ऊपर पाया है काफी काम करने लेकिन यहाँ जो मैंने पाया इसे और अधिक आसान जा रहा कोड अद्यतन किया जाता है।

@property FBSDKProfilePictureView *pictureView; 

if ([FBSDKAccessToken currentAccessToken]) { 


    self.pictureView=[[FBSDKProfilePictureView alloc]init]; 

    [self.pictureView setProfileID:@"me"]; 
    [self.pictureView setPreservesSuperviewLayoutMargins:YES]; 
    [self.pictureView setPictureMode:FBSDKProfilePictureModeNormal]; 
    [self.pictureView setNeedsImageUpdate]; 
    [self performSelector:@selector(getUserImageFromFBView) withObject:nil afterDelay:1.0]; 

} 

-(void) getUserImageFromFBView 
{ 
UIImage *image = nil; 

for (NSObject *obj in [self.pictureView subviews]) { 
    if ([obj isMemberOfClass:[UIImageView class]]) { 
     UIImageView *objImg = (UIImageView *)obj; 
     image = objImg.image; 
     break; 
    } 
} 
[self.profilePic setImage:image forState:UIControlStateNormal]; 
} 

उम्मीद है कि इससे मदद मिलती है। यहां प्रोफ़ाइल प्रोफ़ाइल लोड होने की प्रतीक्षा करने के लिए मैंने 1 सेकंड देरी डाली है।