SDK 4.0을 사용하여 iPhone 용 응용 프로그램을 작성하고 있습니다. 다운로드, 크기 조정 및 많은 이미지를 하나씩 보여줘야합니다.ASIHTTPRequest의 메소드를 대체합니다.
단순한 ASIHTTPRequest
으로 시도했지만 매우 비싼 것으로 나타났습니다. 그래서 ASIHTTPRequest
을 상속받은 서브 클래스를 만들고 requestFinished
을 오버라이드하려고합니다. 나는 그것에 문제가있다. 음 .. 어떻게 든 내 이미지를 내 UIImageView
에 설정해야합니다. 나는 그것을 올바르게하는 방법을 전혀 모른다.
내 하위 클래스에 UIImage
속성을 만들고 크기를 조정 한 다음 요청에서 가져 오려고했습니다. 아아, 문제가 발생합니다. 내가 받고있어 EXC_BAD_ACCESS. 그 방법은 동시성으로 인해 위험 할 수도 있습니다. 그 일을 쉽고 안전하게 할 수있는 방법이 있습니까? 그 이미지를 구문 분석하여 NSData
으로 생각하고 어떻게 든 요청 응답으로 전환했습니다.
편집 : JosephH : 나는 당신이 거기서 쓴 것을했습니다. 그것은 작은 그림을 도왔다 - 나는 크기를 조정할 필요가 없다. 그래서 크기를 조정할 때 다시 뒤쳐졌습니다.
#import "BackgroundHTTPRequest.h"
@implementation BackgroundHTTPRequest
@synthesize myimg;
-(UIImage *)imageWithImage:(UIImage*)imagee scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
UIGraphicsBeginImageContextWithOptions(newSize, YES, [UIScreen mainScreen].scale);
[imagee drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)requestFinished{
NSData *responseData = [self responseData];
UIImage *tempimg = [UIImage imageWithData:responseData];
CGFloat ratio = tempimg.size.height/tempimg.size.width;
CGFloat widthmax = 320;
CGFloat heightmax = widthmax * ratio;
myimg = [self imageWithImage:tempimg scaledToSize:CGSizeMake(widthmax, heightmax)];
[super requestFinished];
}
- (void)dealloc{
self.myimg = nil;
[super dealloc];
}
@end
그리고 그런 일이 몇 가지 코드 :
[self.myrequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
이 도움이되지 않았다
- (IBAction)grabURLInBackground:(NSURL *)url
{
[self.myrequest setDelegate:nil];
[self.myrequest cancel];
[self.myrequest release];
self.myrequest = [[BackgroundHTTPRequest requestWithURL:url] retain];
[myrequest setDelegate:self];
[myrequest startAsynchronous];
}
- (void)requestFinished:(BackgroundHTTPRequest *)request
{
// Use when fetching binary data
if ((NSNull *)self == [NSNull null]){
[request clearDelegatesAndCancel];
} else {
if (self.image)
self.image.image = myrequest.myimg;
}
self.myrequest = nil;
}
플러스 추가 라인 여기에 몇 가지 코드입니다.