I have some doubt about how NSString is released in ARC Mode.
I would like to know if have something i can do to release a nsstring when i want in arc mode.
Take a look at this code:
__block NSString *strImage = nil;
dispatch_sync(backGround, ^{
// Convert NSData To NSString.
strImage = [UIImagePNGRepresentation([UIImage imageWithData:reg.photoData]) base64Encoding];
});
// Prepare Request Operation.
NSString * strPost = @"myUrl";
strPost = [strPost stringByAppendingFormat:@"}&image="];
strPost = [strPost stringByAppendingFormat:@"%@",strImage];
//NSLog(@"POST: %@",strPost);
// setting up the URL to post to
NSString *urlString = @"myURLAPI";
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:[strPost dataUsingEncoding:NSUTF8StringEncoding]]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
//MY DOUBT ABOUT RELEASE THIS STR
@autoreleasepool {
strImage = nil;
}
is it correct or have a best way to do it? Thank you
1 Answer 1
Also @autoreleasepool
does not look are the objects create outside of it scope, just the object used within. Also if using are there is not need for the @autoreleasepool
at all, since the scope variables will be released and nilled at the end of the scope.
-
\$\begingroup\$ so when my function end the str compiler knows that this str need to release? dont i need the autorelease? \$\endgroup\$Juan Munhoes– Juan Munhoes2013年08月30日 21:30:59 +00:00Commented Aug 30, 2013 at 21:30