从NSMutableArray获取图像URL

时间:2013-05-08 20:52:20

标签: objective-c cocoa-touch uiimage nsmutablearray

我正在尝试从pictureURLNSMutableArray或仅imageview获得subView。我正在获取URL,我可以在Xcode输出中看到它。这就是我所拥有的:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    responseData = [[NSMutableData data] retain];
    picArray = [[NSMutableArray alloc] init];
    return self;
}

- (void)viewDidLoad {
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    NSString *user = [settings valueForKey:@"username"];
    NSString *post = [NSString stringWithFormat:@"username=%@&profilePic=dummy", user];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://URL.com/get_pic.php"]];
    NSString *postLen = [[NSString alloc] initWithFormat:@"%d", [post length]];
    [request setValue:postLen forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (UIViewController *)profileController {
    NSURL *imgURL = [NSURL URLWithString:[picArray objectAtIndex:0]]; // put your particular index where your image url in your array...
    NSData *imgData = [[NSData alloc] initWithContentsOfURL:imgURL];
    UIImage *img = [UIImage imageWithData:imgData];
    UIView *userPicView = [[UIView alloc] initWithFrame:CGRectMake(80, 50, 160, 160)];

    // Sets image as background image
    [userPicView setBackgroundColor:[UIColor colorWithPatternImage:img]];

    // adding subview
    [self.view addSubview:userPicView];

    userPicView.tag = 7;

    // release the newView as -addSubview: will retain it
    [userPicView release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //stop indicator on status bar
    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;

    NSLog(@"update reply");

    // NSString *newStr = [NSString stringWithUTF8String:[responseData bytes]];
    NSString *newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(newStr);

    if ([newStr length] != 0) {
        NSArray *list = [newStr componentsSeparatedByString:@","];
        picArray = [list mutableCopy];
    } else {
        // [userArray dealloc];
        picArray = [[NSMutableArray alloc] init];
}

0 个答案:

没有答案