Received memory alert in dispatch_async

This is the code I write in cellForRowAtIndexto upload an image:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    @autoreleasepool {
        __block UIImage * img;
        __block NSData *data;
        if(![messageDocument.SmallImageURL isEqual:@""])
        {
            data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:messageDocument.SmallImageURL]];
            img = [UIImage imageWithData:data];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            imgUser.image = img;
            img = nil;

            imgUser.contentMode = UIViewContentModeScaleAspectFill;
            CGSize size;
            if(imgUser.image.size.width > imageFrame.size.width || imgUser.image.size.height > rectImage.size.height)
            {
                if(imgUser.image.size.width < imageFrame.size.width)
                {
                    rectImage.size.width = imgUser.image.size.width;
                }

                if(imgUser.image.size.height < rectImage.size.height)
                {
                    rectImage.size.height = imgUser.image.size.height;
                }

                size = CGSizeAspectFit(imgUser.image.size, rectImage.size);
                imgUser.frame = CGRectMake(imgUser.frame.origin.x, rectImage.origin.y, size.width, size.height);

                height = imgUser.frame.size.height;
            }
            else
            {
                imgUser.frame = CGRectMake(imageFrame.origin.x, imageFrame.origin.y, imgUser.image.size.width, imgUser.image.size.height);
                height = imgUser.image.size.height;

            }

            CGPoint contentOffset = tableMessageDetail.contentOffset;
            [tableMessageDetail beginUpdates];
            [tableMessageDetail endUpdates];
            [tableMessageDetail setContentOffset:contentOffset];
        });

            messageDocument.Pic = data;
            data = nil;
        if(messageDocument.Pic != nil)
        {
            Attachment *attachment = [Attachment new];
            attachment.DocId = messageDocument.DocId;
            attachment.DocURL = messageDocument.DocURL;
            attachment.ImageId = messageDocument.ImageId;
            attachment.MessageId = messageDocument.MessageId;
            attachment.SmallImageURL = messageDocument.SmallImageURL;
            attachment.OriginalFileName = messageDocument.OriginalFileName;
            if([messageDocument.DocURL isEqual:@""])
            {
                NSArray *attachmentArray = [messageDocument.SmallImageURL componentsSeparatedByString:@"/"];
                NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
                attachment.AttachmentName = attachmentName;
            }
            else
            {
                NSArray *attachmentArray = [messageDocument.DocURL componentsSeparatedByString:@"/"];
                NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
                attachment.AttachmentName = attachmentName;
            }

            attachment.Pic = messageDocument.Pic;
            [[CommonModel shared]CreateAttachment:attachment];
            [[CommonModel shared]UpdateMessageDocumentPic:messageDocument];

            attachment = nil;
        }
    }
});  

But if there are more than 6 images, I get a memory exception with the following message on the console:

Message from the debugger: completed due to a memory problem

+4
source share
2 answers

. , , . , , png, , .

, , ( , ), .

Xcode. , .

+4

@Marek R, . -, , URL-.

var cache = NSCache()

func imageForUrl(urlString: String, completionHandler:(image: UIImage?, url: String) -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {()in
    var data: NSData? = self.cache.objectForKey(urlString) as? NSData

    if let goodData = data {
        let image = UIImage(data: goodData)
        dispatch_async(dispatch_get_main_queue(), {() in
            completionHandler(image: image, url: urlString)
        })
        return
    }

    var downloadTask: NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlString)!, completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
        if (error != nil) {
            completionHandler(image: nil, url: urlString)
            return
        }

        if data != nil {
            let image = UIImage(data: data)
            self.cache.setObject(data, forKey: urlString)
            dispatch_async(dispatch_get_main_queue(), {() in
                completionHandler(image: image, url: urlString)
            })
            return
        }

    })
    downloadTask.resume()
})

}

-, imageFromUrl, :

imageForUrl("http://useYourLinkHere/image.png", completionHandler:{(image: UIImage?, url: String) in
    self.imgUser.image = image!
})

        imgUser.contentMode = UIViewContentModeScaleAspectFill;
        CGSize size;
        if(imgUser.image.size.width > imageFrame.size.width || imgUser.image.size.height > rectImage.size.height)
        {
            if(imgUser.image.size.width < imageFrame.size.width)
            {
                rectImage.size.width = imgUser.image.size.width;
            }

            if(imgUser.image.size.height < rectImage.size.height)
            {
                rectImage.size.height = imgUser.image.size.height;
            }

            size = CGSizeAspectFit(imgUser.image.size, rectImage.size);
            imgUser.frame = CGRectMake(imgUser.frame.origin.x, rectImage.origin.y, size.width, size.height);

            height = imgUser.frame.size.height;
        }
        else
        {
            imgUser.frame = CGRectMake(imageFrame.origin.x, imageFrame.origin.y, imgUser.image.size.width, imgUser.image.size.height);
            height = imgUser.image.size.height;

        }

        CGPoint contentOffset = tableMessageDetail.contentOffset;
        [tableMessageDetail beginUpdates];
        [tableMessageDetail endUpdates];
        [tableMessageDetail setContentOffset:contentOffset];

        messageDocument.Pic = data;
        data = nil;
    if(messageDocument.Pic != nil)
    {
        Attachment *attachment = [Attachment new];
        attachment.DocId = messageDocument.DocId;
        attachment.DocURL = messageDocument.DocURL;
        attachment.ImageId = messageDocument.ImageId;
        attachment.MessageId = messageDocument.MessageId;
        attachment.SmallImageURL = messageDocument.SmallImageURL;
        attachment.OriginalFileName = messageDocument.OriginalFileName;
        if([messageDocument.DocURL isEqual:@""])
        {
            NSArray *attachmentArray = [messageDocument.SmallImageURL componentsSeparatedByString:@"/"];
            NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
            attachment.AttachmentName = attachmentName;
        }
        else
        {
            NSArray *attachmentArray = [messageDocument.DocURL componentsSeparatedByString:@"/"];
            NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
            attachment.AttachmentName = attachmentName;
        }

        attachment.Pic = messageDocument.Pic;
        [[CommonModel shared]CreateAttachment:attachment];
        [[CommonModel shared]UpdateMessageDocumentPic:messageDocument];

        attachment = nil;
    }

, , , : "resizeImageFrame()" "loadModel()", .

!

+1

Source: https://habr.com/ru/post/1617935/


All Articles